Can not make DDE connection using python. win32ui appears not working - dde

I am new to Python (version 2.7). I have been using for a long time some excel workbooks that uses DDE function to capture "real-time market data" - that is served by one Market Data Provider ("Matriks", if matters).
In a bit to simplify my overall process flow, I decided to bypass this excel workbook thing for data capture, and instead, I decided to use python codes to perform the same task.
The problem is that,
1) I get "error: The server could not be created" when I run the code (provided below) using Pythonwin.
2) and I get "ImportError: This must be an MFC application - try 'import win32ui' first" when I run this very simple code using IDLE.
(and of course, I downloaded and run win32ui.)
Any help is appreciated.
# DDE code
# excel equivalent of this code is: =MTX|DATA!EURUSD.SON
import win32ui
import dde
s=dde.CreateServer()
s.Create("MTX")
c=dde.CreateConversation(s)
c.ConnectTo("DATA","SON")
c.Connected()
c.Request("EURUSD")
# returns
# Traceback (most recent call last):
# File "C:\IQTrader\_script\_obj\DDEClient.py", line 12, in <module>
# import dde
# ImportError: This must be an MFC application - try 'import win32ui' first

The issue is resolved. The very basic reason for the error is the pywin module.
For those facing with the same problem:
1.Delete pythonwin and pywin32_system32 folderes entirely (presumably under C:\Python27\Lib\site-packages)
2.Check your pywin32 version; it should be 214 (not 218) for those using v2.7
3.Download pywin32-214.win32-py2.7 from appropriate resources (one is this: http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/ )
4.Everything should be fine.

I had a similar problem. I had to:
uninstall Python 2.7 64-bit
install Python 2.7 32-bit
use the pywin32 version 214
Run cmd.exe as administrator so that the c.ConnectTo() connects successfully
However, I am still having the weird problem that I must have Excel open at the same time with a cell holding the value =MTX|DATA!EURUSD.SON for the python script to return a valid quote. Otherwise, I just get "N/A" as the return value.
#Aykut did you run into this issue as well?

Related

Loading dll using cytpes returns WinError 127

We use a python script to run a camera as part of an experiment. In practice, the python script uses ctypes to load a dll and this is what is used to access the camera functionality. We recently upgraded to a Windows 10 computer, and I now no longer seem to be able to load the dll.
A stripped down version of the relevant script as follows:
PARENT_DIR = os.path.dirname(os.path.realpath(__file__))
os.add_dll_directory(PARENT_DIR)
x = ct_util.find_library('picam_64bit')
self.picam = ct.WinDLL(x)
On the fourth line, the following error is returned:
[WinError 127] The specified procedure could not be found
A few further notes:
The camera works with the manufacturer-supplied executable.
The code was originally written in Python 2.7 and has been migrated to Python 3.8. The same error occurs in both.
This question is clearly relevant. I have made sure that only one version of Python is installed at a time (right now, 3.8), and there is only one version of the dll on the computer.
The third line returns a sensible value, so Python can clearly `see' the DLL, just not load it.

IAC-protocol interface error on python 3

I would like to work with excell sheets (.xls likely per .ods conversion) via python while maintaining all of the sheet's original content. Unlike xlutils (http://www.python-excel.org/) the iac-protocol (http://pythonhosted.org/iac-protocol/index.html) seems to me to be more fit/elegant tool to maintain sheet's style,formulas,dropboxes etc. One of the steps to launch iac's server or interpreter (iacs/iaci) is to initialize the interface which consists among others of this command:
import iac.app.libreoffice.calc as localc
While import iac.app.libreoffice works fine
moving to calc level
import iac.app.libreoffice.calc
throws following error
import iac.app.libreoffice.calc
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.4/site-packages/iac/app/libreoffice/calc.py", line 11, in
from uno import getComponentContext
ImportError: cannot import name 'getComponentContext'
From what I've learned so far on this forum it might be linked to method name duplicity between two modules. This is where I am stuck. How do I learn which other module has such name of a method and how to fix it? Both iac-protocol and unotools are modules downloaded via pip3. I did not created method of such name in any script.
Thank you in advance for any advice!
Python3.4 on Scientific Linux release 7.3 (Nitrogen) LibreOffice 5.0.6.2 00(Build:2)
Some questions to narrow down the problem:
Did you start libreoffice listening on a socket first?
Did you import anything else before import iac.app.libreoffice.calc?
What happens when you start python in a terminal and enter from uno import getComponentContext?
I installed iac-protocol on Linux Mint and was able to import iac.app.libreoffice.calc and then use it. The installation process was complex, so I wouldn't be surprised if there is some problem with how your packages were installed, or possibly it does not work on RHEL-based systems. For one thing, it required me to install gnumeric.
The Calc "Hello World" code that worked for me is as follows.
libreoffice "--accept=socket,host=localhost,port=18100;urp;StarOffice.ServiceManager" --norestore --nofirststartwizard --nologo --calc &
python3
>>> import iac.app.libreoffice.calc as localc
>>> doc = localc.Interface.current_document()
>>> sheet = doc.getSheets().getByIndex(0)
>>> cell = sheet.getCellByPosition(0,0)
>>> cell.setString("Hello, World!")
One more thought: Have you considered using straight PyUNO starting from import uno instead of a wrapper library? That would avoid dependency on some of the extra libraries which may be causing the problem. Also there is better documentation for straight PyUNO.

Read NetCDF file from Azure file storage

I have uploaded a file to my Azure file storage account and created a SAS (shared access signature). Let's pretend the file in question is called fileA.nc
Now, with Python3, I am attempting to read fileA.nc:
from netCDF4 import Dataset
url ='https://<my-azure-resource-group>.file.core.windows.net/<some-file-share>/fileA.nc<SAS-token>';
dataset = Dataset(url)
print(dataset.variables.keys())
The above code does not work, instead giving me the following error:
Traceback (most recent call last): File "yadaYadaYada/test.py", line
8, in
dataset = Dataset(url) File "netCDF4/_netCDF4.pyx", line 1848, in netCDF4._netCDF4.Dataset.init (netCDF4/_netCDF4.c:13983)
OSError: NetCDF: Malformed or unexpected Constraint
This is line 8:
dataset = Dataset(url)
I know the URL provided works. If I paste it into the browser, the file downloads...
I have checked the netCDF4 documentation, which says this:
Remote OPeNDAP-hosted datasets can be accessed for reading over
http
if a URL is provided to the Dataset constructor instead of a filename.
However, this requires that the netCDF library be built with OPenDAP
support, via the --enable-dap configure option (added in version
4.0.1).
However, I have no idea how to tell if when Pycharms installed netcdf4, it used the --enable-dap argument, but I cannot imagine why it would not. Besides, if I stick in a url which points to some HTML, I get the HTML in the error dump and so from that I would think netcdf4 is actually trying to load a remote dataset and so the problem is somewhere else.
I'd really appreciate some help here. Maybe someone knows of another Python 3 netCDF library that will allow me to load my datasets from Azure?
UPDATE
Okay, I can now confirm that the python netcdf4 library does come with --OPenDAP enabled:
Hello again, netCDF4 1.0.4 with OpenDAP support is now available in
the conda respoitory on Unix. To install: $ conda install netcdf4
Ilan
I have found a solution. It turns out that you cannot read directly from an Azure File share, even though when you paste the link to a file in the browser, the file begins to download.
What I needed to do was to mount the File Share on my OS. In my case, I was using Windows but this can be done with Linux, too. The following code should be modified accordingly and then put into Command Prompt:
net use <drive-letter>: \\<storage-account-name>.file.core.windows.net\<share-name>
example :
net use z: \\samples.file.core.windows.net\logs
Once the File Share is mounted, you can read from it as if it were an external HDD. You may need to add permission, but I didn't.
Here is the link to the documentation for mounting the File Share: Documentation

importing ecoinvent 3.2 with brightway

I am having some trouble importing Ecoinvent 3.2 with Brightway2, I was following the example notebook:
from brightway2 import *
fp = 'D:\LCAdb\e_3.2_cutoff_lci\datasets'
projects.set_current("myproject")
bw2setup()
ei = SingleOutputEcospold2Importer(fp, "ecoinvent 3.2 cutoff")
I get a warning message that seems to block the import process
C:\Users\###\Anaconda3\envs\bw3\lib\site-packages\bw2data\project.py:157:
UserWarning:
Read only project
This project is being used by another process and no writes can be
made until:
1. You close the other program, or switch to a different project, and
2. You call projects.enable_writes and get the response True.
If you are **sure** that this warning is incorrect, call
`projects.enable_writes(force=True)` to enable writes.
warnings.warn(READ_ONLY_PROJECT)
if I run
projects.enable_writes(force=True)
I get another a persmission error
PermissionError Traceback (most recent call
last) in ()
----> 1 projects.enable_writes(force=True)
C:\Users\###\Anaconda3\envs\bw3\lib\site-packages\bw2data\project.py
in enable_writes(self, force)
234 """Enable writing for the current project."""
235 if force:
--> 236 os.remove(os.path.join(self.dir, "write-lock"))
237 self.read_only = not self._lock.acquire(timeout = 0.05)
238 if not self.read_only:
PermissionError: [WinError 32] El proceso no tiene acceso al archivo
porque está siendo utilizado por otro proceso:
'C:\Users\###\AppData\Local\pylca\Brightway3\myproject.4da39212894ad06eb7c95810f8a2a6b0\write-lock'
the winerror translated would be something like "the process does not have access to the file because the file is being used by other process"
I do not have other Brightway environments running at the same time and I have recently updated Brightway2 so I do not know where the problem may be. Any ideas?
thanks!
UPDATE1: I have installed brightway2 in a different computer and I have found the same warning message. Despite the message, the import seems to be correct. once the database is loaded and written, if I open the project again the database is still there.
In the previous laptop the process of importing seems to be too much for the machine (an ASUS S56CB with windows 10 and 6 GB RAM). After 40 min waiting for the result I usually despair and kill it. I will give a try reinstalling python...
See how do I resolve errors about read-only projects?.
The locking in Brightway2 is done with the fasteners library. If you have two python interpreters running, or don't exit a ipython notebook correctly (you should shutdown or restart a kernel before closing the process completely), there will be a lock file in the project directory, and you will get errors about read-only projects. By the way, just closing a ipython notebook is not enough to shut down the python kernel or release the lock.
It is difficult to diagnose why these errors could be occurring with what you have reported. Try the following:
1) See if you can import ecoinvent into a new project.
2) Try restarting your computer.
3) If you can't get anything to work, you can delete the file C:\Users\###\AppData\Local\pylca\Brightway3\myproject.4da39212894ad06eb7c95810f8a2a6b0\write-lock in the command line or in an explorer window.
4) Do what #Mpa suggest in the next answer, and set projects.read_only = False.
I have run into this in the past, surely because of the reasons #Chris evoked.
You can use projects.read_only = False to force-write data. Please make sure that this is really what you want to do. You will not want to do this, for example, if you are accessing the same project through two different kernels that may try to write data at the same time.

TinyTag import error python 3.3

I have been trying to import tinytag into python to be able to read mp3 tags but I keep receiving the same error. This is the code I am running
from tinytag import TinyTag
tag = TinyTag.get('/some/music.mp3)
print(tag.album)
and the error I recieve from this is
ImportError: No module named 'tinytag'
If anyone could give me any information on how to fix this would be greatly appreciated or can suggest another reader to use that is compatible with python 3.
Like you, I'm new to Python and I struggled with this but I worked it out eventually. I'm sure there is better way, but this worked (on windows, with my examples)
I installed a python module called easy_install (bundled with
setuptools). you can Google this. In the directory \Python26\Scripts you should see an exe file called easy_install if this has worked
Then I downloaded TinyTag to my pc eg
\downloads\tinytag-0.6.1.tar.gz
Then in note pad I wrote a small text file called myinstall.bat with
the contents
easy_install C:/downloads/tinytag-0.6.1.tar.gz
pause
then saved it into \Python26\Scripts and ran it (the pause keeps the
window open so you can see it worked)
Subsequently I started using some software called JetBrains to code with (it's commercial but there is a free edition) and that has an install tool built in which is even easier) I hope this helps

Resources