I am trying to use ActiveX automation with python to control Audio Precision ATS-2. I am using pywin32 32-bit Windows XP with Python 2.7.
I installed the audio Precision software. Then I used makepy utility which found Audio Precision in the list and created a wrapper for it. Everything is going well until I attempt to call dispatch function abd I am stuck.
from guides I found online,
win32com.client.Dispatch("Excel.Application")
What should I put in the area that says "Excel.Application"?
How can I find this?
When I used OLE/COM Object viewer, I see Audio Precision entries under type library, but I am unsure how does the information in it could help me.
I have attempted few things on my own, but got almost no result. The best result I got was when I entered the CLSID in dispatch function, which I found in the wrapper object makepy created.
win32com.client.Dispatch("{80EC6E76-D94F-48EB-8F4C-05BDD7850BF1}")
which gave me the following error:
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\win32com\gen_py\APtest.py", line 5, in <module>
xlApp = win32com.client.Dispatch("{80EC6E76-D94F-48EB-8F4C-05BDD7850BF1}")
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 108, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 85, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2147221164, 'Class not registered', None, None)
I have:
looked under the registry, and the ID is indeed not registered anywhere. There are few Audio Precision entries, but doesn't look like it has anything that could fix this.
attempted reinstalling the Audio Precision software, but it didn't help.
attempted to register the dll objects in the Audio Precision control(ATS 1.60) software with regsrv32, but all gave errors and would not register(which I suspect aren't the right ones I need to register)
How can I fix this?
Possible way to solve this:
Identify the Audio Precision ATS-2 dll you want to call. E.g. "C:\path\my.dll"
Now go to the registry and look for "C:\path\my.dll". You'll find a few things around but what you really want is an entry in CLSID...\InprocServer32 folder, or something similar
There will be a ProgID entry as well, something like "AudioPrecison.ATS2"
Now in Python try win32com.client.Dispatch("AudioPrecison.ATS2")
To use the pywin32 module with an activex class you need to generate a wrapper file. To do this locate your makepy.py file in C:\Python27\Lib\site-packages\win32com\client (or your simmilar dir). Then run it, locate the NAME of your activex class then run it. It should generate a wrapper file in the gen_py folder C:\Python27\Lib\site-packages\win32com\gen_py with the CLSID in it. You can then access it like the excel example and the same way you would in other languages.
I had the same issue. I didn't know where to get that, which from what I can gather is the name of the CoClass (which I believe is the wrapper for the dll) created by the makepy program (maybe I'm wrong). After I read the answer by #NotAUser I found it, but when I inspected the file created by the makepy program, and right before the last class, (if more than one are created) I found the following:
from win32com.client import CoClassBaseClass
# This CoClass is known by the name 'ZKFPEngXControl.ZKFPEngX'
class ZKFPEngX(CoClassBaseClass): # A CoClass
So, there, in the same file there was the name I needed to use.
Related
[autoreload of R17_Plot_profiles failed: Traceback (most recent call last):
File "/home/jdrevon/anaconda3/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 244, in check
superreload(m, reload, self.old_objects)
File "/home/jdrevon/anaconda3/lib/python3.7/site-packages/IPython/extensions/autoreload.py", line 378, in superreload
module = reload(module)
File "/home/jdrevon/anaconda3/lib/python3.7/imp.py", line 314, in reload
return importlib.reload(module)
File "/home/jdrevon/anaconda3/lib/python3.7/importlib/__init__.py", line 168, in reload
raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
ModuleNotFoundError: spec not found for the module 'R17_Plot_profiles'
]
This problem occurs in Python 3.7 on Spyder when I modify part of my code and I press F9 to execute some parts of it. Even if I execute my main python file which load every sub-python .py codes I have this error message. However, It does not stop the code from running but the file didn't save the modifications made. This is the first times it happens since I imported the 'R17_Plot_profiles.py' python file.
However in the case where I execute the main profile, I have the previous message but I have also the following one :
Reloaded modules: reconstruct, R2_Spice_Kernels, R4_Offset_Correction, R5_State_Vectors_twoway_v2, R6_Foot_Print, R7_Framework_Conversion,
R8_BendAng_ImpParam_up, R8_BendAng_ImpParam_dn, R9_Avg_BendAng_ImpParam, R10_Altitude_above_Ellipsoid, R11_Refractivity_and_Bending_Radius_v2,
R12a_Electron_Density_up, R12b_Electron_Density_dn, R12_Electron_Density_avg, R13_Neutral_Number_Density,
R14_Pressure_and_Temperature, R16_Errors, R17_Plot_profiles,
R3_Read_L2_Data_v2
So I don't know how to solve the autoreload problem. I've tried to downgrade my IPython version to 7.5 like some people tried and succeed but for me It doesn't change anything.
Edit : It's important to notice that if I restart the kernel, the first execution do not show the error message until I modify directly the python code.
I did few modifications yesterday and it seems to be solved. Since the error occurence started to be random at some points, i'm not sure for 100% but it can maybe solve the issue.
The problem could be came from the use of os.chdir function, I replaced it by specifying the entire path to save my figures until to go to the path using os.chdir and then save my figures in it.
It can maybe produce some conflicts of path and repository to find the python script which is no longer in the path where the script is currently working and therefore can't find it in order to save the modifications.
I keep you informed if at some point the error comes back but for now after some trials the error didn't show up anymore.
I'm trying to use the sutime python wrapper to make a date normalizer, that would convert any temporal information in strings into dates in the format YYYY-MM-DD. I've created a class, with rules over the sutime outputs to convert the sutime outputs into the standard format as mentioned above. The program is working properly on my local machine, but when i try to run it on a server I get the jpype._jclass.NoClassDefFoundError. The server is on ubuntu with python2, while my local has windows, with python3.
I've tried to implement the solutions to a similar problem on this https://sourceforge.net/p/jpype/discussion/379372/thread/689d7a9b/ forum, but i'm not sure if i was able to implement these soultions correctly. I've also checked that sutime supports both python3 and python2
I think the issue is with jpype or with the sutime library.
This is the traceback that i got
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "date_normalizer.py", line 38, in __init__
self.sutime = SUTime(jars=self.jar_files, mark_time_ranges=mark_time_ranges)
File "/home/bridgei2i/.local/lib/python2.7/site-packages/sutime/sutime.py", line 57, in __init__
'edu.stanford.nlp.python.SUTimeWrapper')
File "/home/bridgei2i/.local/lib/python2.7/site-packages/jpype/_jclass.py", line 130, in __new__
return _JClassNew(args[0], **kwargs)
File "/home/bridgei2i/.local/lib/python2.7/site-packages/jpype/_jclass.py", line 213, in _JClassNew
javaClass = _jpype.PyJPClass(arg)
jpype._jclass.NoClassDefFoundError: edu/stanford/nlp/python/SUTimeWrapper
Seems likely that the jar file holding edu/stanford/nlp/python/SUTimeWrapper was not found on the server. The specific code that failed was a call to JClass('edu.stanford.nlp.python.SUTimeWrapper') which is a request to load a class from a jar. I would recommend checking the classpath and configuration on the server.
Likely causes are (in order of likelihood)
jar file is not located in the classpath on the server.
The jar file is compiled with a JDK which is newer than runtime environment (though this should generate a different exception).
Some jar file that the class depends on is missing or has the wrong version. (this should produce a different classname in the exception, so it is unlikely.)
A dll for a native portion the jar file is missing or has an incorrect architecture. (rare)
Assuming the jar file is on the server, I would recommend checking the initialization in which the JPype startJVM call is made to see if the path to the jar was correct. It is also possible to examine the loaded classpath using print(jpype.java.lang.System.getProperty('java.class.path')) to see if there is a difference between your local and server machine.
thank you, as you said:Some jar file that the class depends on is missing or has the wrong version. (this should produce a different classname in the exception, so it is unlikely.)
How can I use the navigation-policy-decision-requested (or equivalent) in association with webkit_web_policy_decision_ignore()?
The following is a small outtake form my code (the rest is just a wrapper and settings etc):
def check(view, frame, req, nav, policy):
webkit_web_policy_decision_ignore(TRUE)
...
view.connect("navigation-policy-decision-requested", check)
When I load a new page this error is returned:
Traceback (most recent call last):
File "browser.py", line 17, in check_exec
webkit_web_policy_decision_ignore(TRUE)
NameError: global name 'webkit_web_policy_decision_ignore' is not defined
What I'm trying to achieve is that if a certain address have been given, actions will be taken to prevent it from loading via Python. Any suggestions are welcome in the comments and any additional information you may need will be provided upon request.
Now I'm new to Python so please be specific in your answer, criticism and suggestions.
If you are using pygtk, try policy.ignore().
The object names are mapped slightly differently in pygtk. In python shell you can try after executing from gi.repository import WebKit
print dir(WebKit)
to find corresponding object and in your case
help(WebKit.WebPolicyDecision)
It might be a very simple question. I was running a python code, and I got an error message as such :
File "/home/mbenchoufi/brisket/../brisket/views.py", line 11, in <module>
from influence.forms import SearchForm
ImportError: No module named forms
The problem is first that I have indeed a file called views.py in /home/myname/brisket/ but I don''t understand the notation : /home/myname/brisket/../brisket/views.py
Do I have a path config problem and what does this notation means ?
Btw, a really weird thing is that I have a file called forms.py, in the influence folder, and in this file a I have a class called SearchForm... How can the error message can be ?
This is not a Python-specific notation, it's a UNIX filesystem notation. .. in a UNIX path means "back up one directory," so for example, in this case, /home/myname/brisket/.. is equivalent to just /home/myname.
The reason Python displays the filename in this way might be that your sys.path has actually has /home/myname/brisket/.. in it for some reason. It's not a problem, since Python will be able to follow the ..s in the path just fine.
What this error message is telling you is that, while processing the file /home/myname/brisket/../brisket/views.py (which is the same file as /home/myname/brisket/views.py) there is a line of code
from influence.forms import SearchForm
which caused an error. Specifically, it's an ImportError, meaning that the file influence/forms.py wasn't found (or could not be read) by Python's import mechanism. You should check the value of sys.path in your Python program to make sure that the parent directory of influence/ is in the list, and make sure that the file is readable. (Also make sure that influence/__init__.py exists, though I'm not sure that particular problem would cause the error you're seeing.)
"/home/myname/brisket/../brisket/views.py"
is equivalent to
"/home/myname/brisket/views.py"
The cause might be an entry in you PYTHONPATH, e.g. like
export PYTHONPATH="$HOME/../brisket:$PYTHONPATH"
http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH
The above approach has the benefit of working for other users, while not requiring an absolute path to /home. Write it like
export PYTHONPATH="/home/brisket:$PYTHONPATH"
to get simpler paths
I have created a simple MFC .dll file and I am able to access it via another MFC Application program. I was trying to do the same thing using Python 3.2.2 but after I load my library, It does not detect the function in my dll and gives an error like:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python32\lib\ctypes\__init__.py", line 353, in __getattr__
func = self.__getitem__(name)
File "C:\Python32\lib\ctypes\__init__.py", line 358, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'SayHello' not found
..
I have trying to find a simple program to access my MFC dll file using Python but have got no success as of yet. Just to let everyone know, I have read on ctypes and have been searching lots to get this working. My main aim is to use Python as a scripting language to interface with my MFC C++ dll. I have been reading lots and the closest one I could get to was with PythonWin. Please help in this regard.
Cheers.
I have been able to get a hold of my problem and have solved it. For those who might expect this in the future, I would just like to paste the python script code in order to make your functions accessible in your python scripts. The way to go with your C++ function is obviously to declare this in your .dll file.
extern "C" __declspec(dllexport) int SayHello(int strNo);
and your Python script should go something like this: (getattr() function is required to access your c++ function in your python script)
mydll = cdll.LoadLibrary("Location.dll")
hellofunc = getattr(mydll,'SayHello')
func2.restype = c_int
func2.argtypes = [c_int]
x = func2(34)
print(x)
...
If anyone thinks I might be wrong, please feel free to correct me. I am a beginner in Python, it worked for me this way and I am able to go ahead with my scripting after this.
Hope this helps others.
Cheers.