I have comtypes 0.6.2 installed on Python 2.6. If I try this:
import comtypes.gen
I get:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import comtypes.gen
ImportError: No module named gen
Other imports like import comtypes and import comtypes.client, however, work fine.
What am I doing wrong?
From the name it seems comtypes.gen is generated code? If so, do I need certain preparatory steps before importing? I'm not logged in as administrator. Could that cause code generation to fail?
Edit:
The above problem is solved with a reload(comtypes.gen) (I don't understand how, though). However, now from comtypes.gen import IWshRuntimeLibrary is not working. This symbol should be part of a generated code. So how do I get this code to be generated?
Well, after some experimentation, I have the solution.
I've found that:
Importing comtypes.client automatically creates the comtypes.gen subpackage.
Calling comtypes.client.GetModule("MyComLib") generates a wrapper for "MyComLib".
So the following code did the job for me:
import os
import glob
import comtypes.client
#Generates wrapper for a given library
def wrap(com_lib):
try:
comtypes.client.GetModule(com_lib)
except:
print "Failed to wrap {0}".format(com_lib)
sys32dir = os.path.join(os.environ["SystemRoot"], "system32")
#Generate wrappers for all ocx's in system32
for lib in glob.glob(os.path.join(sys32dir, "*.ocx")):
wrap(lib)
#Generate for all dll's in system32
for lib in glob.glob(os.path.join(sys32dir, "*.tlb")):
wrap(lib)
Having the relevant COM lib wrapped, now I can access IWshRuntimeLibrary just fine.
Perhaps, as it says, the package gen package in comptypes doesn't exist. Check your site-packages folder (C:\Python26\Lib\site-packages on Windows, substitute the C:\Python26 with your installation directory) for a comtypes\gen subfolder.
recently i got the new office, and i had to extend the script of #frederick to generate all the office objects again.
import os
import glob
import comtypes.client
# You may want to change the office path
msoffice=r'C:\Program Files (x86)\Microsoft Office\root\Office16'
#Generates wrapper for a given library
def wrap(com_lib):
try:
comtypes.client.GetModule(com_lib)
except:
print("Failed to wrap {0}".format( com_lib))
sys32dir = os.path.join(os.environ["SystemRoot"], "system32")
#Generate wrappers for all ocx's in system32
for lib in glob.glob(os.path.join(sys32dir, "*.ocx")):
wrap(lib)
#Generate for all dll's in system32
for lib in glob.glob(os.path.join(msoffice, "*.tlb")):
wrap(lib)
for lib in glob.glob(os.path.join(msoffice, "*.olb")):
wrap(lib)
# And a special case for Excel
excel=os.path.join(msoffice,"excel.exe")
wrap(excel)
Related
I have a dll file that I need to import in Python. However when I try to load the assembly I get the error:
ModuleNotFoundError: No module named 'TEST_NET'
The assembly dll file is located in C:\Program Files\TEST APP\APP 7.0 under the name TEST_NET.Core.dll
My code is the following:
app_version = "7.0"
app_library = "C:\Program Files\TEST APP\APP 7.0"
# load app assemblies... replace the path below with the installation
# installation folder for your app installation.
# Import from .NET assemblies (both App and system)
sys.path.append(app_library)
clr.AddReference('TEST_NET.Core')
from TEST_NET.Core import*
The code was working well with a previous app version. But the new app that contains the file has return this issue where the module is not found when trying to import. Any idea what might be wrong here?
Thanks!
use ctypes to import a dll
import ctypes
dll = ctypes.WinDLL (r"C:\Program Files\TEST APP\APP 7.0\TEST_NET.Core.dll")
I'm currently trying to use a library written in C with my Python code but I didn't have much success at all. I'm currently using Windows and have downloaded, saved and installed the library (linked below) to a directory near my project using Cygwin.
As of now, I'm not sure how you load it within your Python code. I followed the instruction in this link: https://github.com/xiph/rnnoise/issues/69 but replaced the library path with a relative path and saved the library inside my project directory. Here's my code and here's the return error
import wave
import os,sys
import ctypes
import contextlib
import numpy as np
from ctypes import util
from scipy.io import wavfile
from pydub import AudioSegment
lib_path = "rnnoise"
lib = ctypes.cdll.LoadLibrary(lib_path)
Here's the version from the instruction:
import wave
import os,sys
import ctypes
import contextlib
import numpy as np
from ctypes import util
from scipy.io import wavfile
from pydub import AudioSegment
lib_path = util.find_library("rnnoise")
if (not("/" in lib_path)):
lib_path = (os.popen('ldconfig -p | grep '+lib_path).read().split('\n')[0].strip().split(" ")[-1] or ("/usr/local/lib/"+lib_path))
lib = ctypes.cdll.LoadLibrary(lib_path)
Traceback (most recent call last):
File "c:/Users/duyba/Documents/CET/Project/vadtest/noisetest.py", line 14, in <module>
lib = ctypes.cdll.LoadLibrary(lib_path)
File "C:\Users\duyba\anaconda3\lib\ctypes\__init__.py", line 459, in LoadLibrary
return self._dlltype(name)
File "C:\Users\duyba\anaconda3\lib\ctypes\__init__.py", line 381, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 1920] The file cannot be accessed by the system
My impression of the instruction version was that they are trying to locate the library in Linux, so I just copied the library to the project folder and didn't know what they are trying to load once the library is located. Also, I think that some environment pathway problems may be at play here since I downloaded and installed this library in Windows.
The library: https://github.com/xiph/rnnoise.
Thanks.
This could be an file access permission error. You might be able to get around it by making a new virtual environment or by uninstalling and reinstalling Python; see this post.
I am using python/selenium in visual studio code. I am trying to import my another python class driverScript which resides in executionEngine module and in the file DriverScript. I have imported as below:
import driverScript from executionEngine.DriverScript
It is producing error:
Traceback (most recent call last):
File "c:/Selenium/Selenium-Python Framework/RK_Practice/Tests/mainTest.py", line 5, in <module>
from executionEngine.DriverScript import driverScript
ModuleNotFoundError: No module named 'executionEngine'
How can I import correctly? Your help is very much appreciated.
If the script you are wishing to import is not in the current directory, you may want to take this approach:
import sys
sys.path.insert(1, '/path/to/script/folder')
import driverScript from executionEngine.DriverScript
If your python file is on the same level in dir, then you can import just by calling:
import filename
If your python file is inside another folder, then you need to create a blank __init__.py file that will help python to understand it's a package. Then you can import that as follows:
from folderName import filename
Depends on where executionEngine is supposed to come from. If it's from a package installable via a package manager such as pip or Anaconda, looks like it's not properly installed. If you installed it yourself, you probably need to add the directory containing executionEngine to your PYTHONPATH, so the Python interpreter can find it. This can be done in the VSCode environment files. See the PYTHONPATH section in https://code.visualstudio.com/docs/python/environments
I have a python code and some nao files(naoqi.py,_inaoqi.so, ...) in a folder in a raspberry pi 3 model B v1.2 with armv7l. my code has some import line:
import sys
from naoqi import ALProxy
import time
import almath
import os
import socket
when i run this code i see "cannot open shared object file: No such file or directory" error from second line :
from naoqi import ALProxy
and in below line in naoqi.py (in line import _inaoqi):
try:
import _inaoqi
except ImportError:
# quick hack to keep inaoqi.py happy
if sys.platform.startswith("win"):
print "Could not find _inaoqi, trying with _inaoqi_d"
import _inaoqi_d as _inaoqi
else:
raise
this file is available, but i see "cannot open shared object file: No such file or directory" error.
Why such an error occurs.
What can i do?
Just dumping the inaoqi files into your program directory isn't sufficient, you have to package them properly as a "python module." Is there an installer available for an inaoqi package, or can it be installed using pip?
Also, if you're running Python on Windows, the .so file isn't going to do you any good. The C or C++ code for the module on Windows will be in a .dll file, so again, check to see if an installer for the module is available for your platform.
I am following the instructions on this blog post, to convert from Mercurial to Git
When I run the script as such:
hg-fast-export.sh -r c:\projects\demoapp
Then it fails with the following error:
Traceback (most recent call last):
File "./hg-fast-export.py", line 11, in <module>
from mercurial import node
ImportError: cannot import name node
And the begining of my hg-fast-export.py looks like this
#!/usr/bin/env python
# Copyright (c) 2007, 2008 Rocco Rutte <pdmef#gmx.net> and others.
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
import sys
# import mercurial libraries from zip:
sys.path.append(r'C:\Program Files (x86)\Mercurial\library.zip')
from mercurial import node
from hg2git import setup_repo,fixup_user,get_branch,get_changeset
from hg2git import load_cache,save_cache,get_git_sha1,set_default_branch,set_origin_name
from optparse import OptionParser
import re
import os
I checked the library.zip file (which is located in C:\Program Files (x86)\Mercurial\, and it contains the following folder structure (in addition to many other files/folders insize library.zip
library.zip
|
---------mercurial
|
----------node.pyc
I am really stumped. I do not know what to do. I have been stuck on this for two days. It maybe something very simple that I am overlooking, but I have not idea what it is. Is it a caching issue? Is it a setup issue? Is it an environment issue?
Please helpl, and thanks :)
You almost certainly have another mercurial package or module in your path somewhere. Since you use sys.path.append() the library.zip file is searched last for the module.
Your best bet is to add library zipfile to the Python module search path at the start:
sys.path.insert(0, r'C:\Program Files (x86)\Mercurial\library.zip')
If this is a package, have you tried placing an __init__.py file? This would make sure that files from your subdirectories can be found. While you would have to change a little bit of code (especially in your import statements), this seems like it should be the way to go.