SOLVED - Cannot find/load .dll module - python

When trying to load a .dll file with
os.add_dll_directory("C:\\path\\to\\file")
library = ctypes.windll.LoadLibrary("C:\\path\\to\\file\\tdcbase.dll")
I get following error message:
FileNotFoundError: Could not find module 'C:\path\to\file\tdcbase.dll' (or one of its dependencies).
Try using the full path with constructor syntax.
I'm trying to run it with Python 3.9.7 on Windows11 (on Windows10 it also didn't work).
Using other Python versions (3.6, 3.7 and 3.8) did not change anything either.
Also, I don't think that there are any dependencies whose directory would need to be added.
There is also a 32-bit version of that file (tdbase.dll). When I try to load this with my 64-bit machine, I get following error message:
OSError: [WinError 193] %1 is not a valid Win32 application
So it seems as if it can recognise that it is the wrong bit number, i.e. it can find the file, but in the case of 64-bits not?
Can anyone see where the problem lies?
EDIT: After installing an additional usb driver, which was needed to speak to the connected instrument, the library could be loaded and everything works fine now.

Related

What does `ImportError: DLL load failed: The operating system cannot run %1` mean?

I have a project written in Python 3.9 and C++ 17, with pybind11 as the glue. It works just fine when running alone or through various debuggers, like VS's debugger and NVIDIA Nsight. However, when running through RenderDoc (a graphics API debugger, with it's own Python bindings which I don't use) I get the following error in the terminal:
ImportError: DLL load failed: The operating system cannot run %1
The error if given out for modules in my project trying to import modules from my project.
At first the error message feels similar to ImportError: DLL load failed: The specified module could not be found, so I investigated in Process Monitor, but the module seems to be found just fine.
So I'm wondering, what on earth does this error message mean? What is the OS trying to do but failing at? What might cause it? How might I debug it?
Some other threads suggest it might be to do with Python 3.8's new add_dll_directory, but I don't see why adding some specific debugger would cause this when everything runs fine without it?
I don't think it is an environment issue, as I've run everything through the same terminal. Also, it ran through RenderDoc just fine before performing various library upgrades, including an upgrade over the Python 3.8 boundary.

Ctypes throws "WindowsError: [Error 193] %1 is not a valid Win32 application", but it's not a 32/64-bit issue

I have a problem loading a Windows DLL in Ctypes, which throws the error:
WindowsError: [Error 193] %1 is not a valid Win32 application
In my case, it's a 32-bit DLL built with VS2012 on Windows 7 64-bit, and on my development machine I can load it fine. I checked that it's 32-bit, using dumpbin /headers:
FILE HEADER VALUES
14C machine (x86)
The problem occurs when I try to load the same DLL via Ctypes on a production VM, which is also Windows 7 64-bit. What I'm doing is:
from ctypes import *
self.dll = CDLL(dllabspath)
I get:
File "C:\Users\user\Desktop\WinPython-32bit-2.7.10.1\.....\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 application
From the other questions I've already tried several things.
This, this, this and this question suggests that my environment has to be the same, i.e. 32-bit Python, 32-bit DLL. This is the case on my development system and the VM I'm testing on. On both, I'm using WinPython 32-bit, latest version. It works on the dev machine, it fails on the VM.
Here, it's related to g++ and a dependency on old Visual Studio runtimes. I compiled everything with VS2012 so I don't think this is applicable here. There is a delayed-loaded dependency on a third party library that needs MSVCR80.dll, but it's delayed-loaded and never called.
I've also installed the Visual C++ 32-bit Runtime on the target machine.
This suggests that the DLL needs to export a C interface, which it does.
I know that the file path / file name to the DLL is correct, as before, there were issues with missing DLL dependencies, where I got a Windows popup. These are gone now.
The error is very generic, rather cryptic. Since it works on the dev machine in the same Python env, I'm assuming it has to do with some dependencies that only a Visual Studio installation can give me?
How can I troubleshoot this properly?
#eryksun commented:
Run under a debugger such as cdb or windbg. Call windll.kernel32.DebugBreak() just before calling CDLL(dllabspath). Set a breakpoint on kernelbase!LoadLibraryExW and resume the thread via g. When it breaks back into the debugger enter pt to execute up to the function return. Then enter !teb to check the LastStatusValue for the thread. This NT status value may be of more help.
Further:
If you prefer to keep the system as clean as possible, try the following: windll.kernelbase.LoadLibraryExW(c_wchar_p(dllabspath), None, 0); status = windll.ntdll.RtlGetLastNtStatus().
Otherwise it requires installing the debugging tools from the SDK. Symbols can be downloaded on demand from Microsoft's symbol server by setting the environment variable _NT_SYMBOL_PATH=symsrv*symsrv.dll*C:\Symbols*http://msdl.microsoft.com/download/symbols, which caches symbols in C:\Symbols
When debugging, this may help:
There are several status codes that produce Win32 error ERROR_BAD_EXE_FORMAT (193). In your case it's STATUS_INVALID_IMAGE_FORMAT (0xC000007B). Possibly in the production VM one of the dependent DLLs that it tries to load is 64-bit. At the breakpoint enter du poi(#esp+4) to print the first argument, which is the unicode path of the DLL it's attempting to load. Also check the stack trace via kc.
Using this hint, I found a dependency on a 64-Bit WinPCAP DLL. Going through everything with DependencyWalker, it looked the same on both machines, complaining about a 64-Bit dependency, but apparently on the fresh machine, the DLL load path was different and it could never find the 32-Bit version.

Error "ImportError: DLL load failed: %1 is not a valid Win32 application"

I have this issue where I try to import cv2 on Python and get the following error message.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: %1 is not a valid Win32 application.
I do understand there are many posts about this where it is suggested that the bitness of the package is different from the Python package.
However, everything I am running is 64 bits. I am on Windows 7 64 bits, I have the winpython 2.7.3.3, 64 bits distribution, and I compiled OpenCV in 64 bits with the instruction provided here and placed the cv2.pyd DLL file in the Lib/site-packages folder of the Python interpreter.
Unfortunately, the suggestion of using the 32 bits version of Python isn't working for me any more as I have to handle NumPy arrays too large for 32 bits.
The only thing missing was to add the new NumPy binaries path (C:\opencv\build\bin\Release) to the Windows PATH environment variable, restart the Python interpreter.
Everything seems to be working fine now!
The ImportError message is a bit misleading because of the reference to Win32, whereas the problem was simply the OpenCV DLLs were not found.
This problem was solved by adding the path the OpenCV binaries to the Windows PATH environment variable (as an example, on my computer this path is: C:\opencv\build\bin\Release).
You could try installing the 32 bit version of OpenCV.
I just hit this and the problem was that the package had at one point been installed in the per-user packages directory. (On Windows.) aka %AppData%\Python. So Python was looking there first, finding an old 32-bit version of the .pyd file, and failing with the listed error. Unfortunately pip uninstall by itself wasn't enough to clean this, and at this time pip 10.0.1 doesn't seem to have a --user parameter for uninstall, only for install.
tl;dr Deleting the old .pyd from %AppData%\python\python27\site-packages resolved this problem for me.
Or you have to rebuild the cv2 module for Windows 64 bit.
This error can also appear when Python versions are mixed:
For example, if any of the DLL file to be loaded has been compiled using Python 2.7.16 and you try to import with Python 2.7.15 the error ImportError: DLL load failed: %1 is not a valid Win32 application. is thrown.
This is at least what I've found to be the problem in my case.
All you have to do is copy the cv2.pyd file from the x86 folder (C:\opencv\build\python\2.7\x86\ for example) to C:\Python27\Lib\site-packages\, not from the x64 folder.
I had the same error as the one mentioned in previous answers, but it happens only when I use pyinstaller.
I did the following in my Poetry venv:
poetry run python -m pip install pypiwin32
And it worked for me. I am using
python 3.8.10
pyinstaller 4.7
# Those are the versions that worked for me
pypiwin32 223
pywin32 303
pywin32-ctypes 0.2.0
When I had this error, it went away after I my computer crashed and restarted. Try closing and reopening your IDE. If that doesn't work, try restarting your computer. I had just installed the libraries at that point without restarting PyCharm when I got this error.
I never closed PyCharm first to test, because my blasted computer keeps crashing randomly... I am working on that one, but it at least it solved this problem... little victories... :).

WindowsError: [Error 127] The specified procedure could not be found

What does this error mean? I have trouble googling any docs. The code worked fine and suddenly broke after restarting windows. I looked in the process monitor and the DLL is found and accessed without any permission errors
File "c:\Python27\lib\ctypes\__init__.py", line 435, in __getattr__
dll = self._dlltype(name)
File "c:\Python27\lib\ctypes\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 127] The specified procedure could not be found
I have faced the same problem while working with Python 2.7 and this is what I found:
-- Problem was coming because I have installed multiple versions of python (Python3.2, Python3.5 etc).
-- Because of multiple versions, at runtime there was multiple DLLs with same name (in all python versions library). So it was unable to figure out which one to load.
Solution:
-- I have uninstalled all python versions except one I needed (Python2.7) at time. Then run the program and then everything was working fine for me.
- In case someone is figuring a Quick-Fix for again able to download using conda:
I've a exactly same issue while downloading packages using conda install xyz using "CMD" as well as "PowerShell" both. Tried a lot to figure out what was causing problem but couldn't get relevant help. However, just by-chance or hit & trial, I executed the command on "Anaconda PowerShell Prompt" and it worked! Don't know the exact problem but for now I'm ready to go ahead with my packages downloaded :)
This error can be caused if you compile your dll with one name and then change its name afterwards. I don't know why, but this was my problem. If you have changed the name try changing it back to its original name
Problem coming only due to
OSGeo4W and QGIS installation
Uninstall this.
or reinstall with
Select Express Web-GIS Install and click next.
In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default, may be unchecked safely.
I encountered the same problem on Win10. After updating python to 3.8 this problem disapeared.

How to use OpenCV in Python?

I have just installed OpenCV on my Windows 7 machine. As a result, I get a new directory:
C:\OpenCV2.2\Python2.7\Lib\site-packages
In this directory, I have two files: cv.lib and cv.pyd.
Then I try to use the opencv from Python. I do the following:
import sys
sys.path.append('C:\OpenCV2.2\Python2.7\Lib\site-packages')
import cv
As a result I get the following error message:
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
What am I doing wrong?
ADDED
As it was recommended here, I have copied content of C:\OpenCV2.0\Python2.6\Lib\site-packages to the C:\Python26\Lib\site-packages. It did not help.
ADDED 2
My environment variables have the following values:
Path=C:\Program Files\MiKTex\miktex\bin;C:\OpenCV2.2\bin;C:\Python26;
PYTHONPATH=C:\OpenCV2.2\Python2.7\Lib\site-packages
Do I need to change something? Do I need to add something?
ADDED 3
I think my question is general: How to use a library? Probably I need to find a *.ddl file somewhere? Then I need to use the name of the directory containing this file as a value to some environment variables? Or maybe I need to use sys.addpath? I also need to know how the way to call the library is related to the name of the file that contains the library.
ADDED 4
It is interesting that when I type import cv, I get:
ImportError: DLL load failed: The specified module could not be found.
But when I type import opencv I get:
ImportError: No module named opencv
ADDED 5
It has been suggested that I usthe e inconsistent version of python. In more details, OpenCV tries to use Python2.7 and I had Python2.6. So, I have installed Python 2.7. It makes difference. Now I do not have the old error message, but I have a new one:
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import
ADDED 6
I have managed to resolve the problem by installing numpy. It took some time because I did not realized that there are different numpy installer corresponding to different versions of Python. Some details can be found in my answer to my own question (see bellow).
The problem was resolved. The following steps has been done:
A new version of python (version 2.7) has been installed.
After that I still was unable to run OpenCV because I had some problems with the numpy library.
I tired to install numpy but the installer did not see my new version of the Python.
I deleted the old version of Python as well as links to the old version in the Path system vatriable.
After that numpy installer was not able to finish the installation.
I have realized that I need to run another numpy installer that is associated with the Python 2.7. It can be found here.
Finally everything worked. I was able to "import cv".
I suspect you have the same problem I've run into. If you have a 64-bit version of Python, it cannot load 32-bit DLLs. OpenCV currently only ships 32-bit binaries. If you want 64-bit .pyd and .dll files, you have to compile them yourself. There are some instructions on the OpenCV Wiki, but it's not for the faint of heart. Expect to have a substantial time investment.
The easiest solution is to:
Uninstall 64-bit Python
Install a 32-bit distribution.
The PythonXY distribution includes pyopencv -- a good set of OpenCV hooks. The only limitation is that it's 32-bit, so don't make plans to process gigapixel astronomy data with it! ;)
If you must have the 64-bit version, follow these instructions to get it OpenCV to compile with Visual Studio 2010. There's a discussion on stackoverflow that describes building 64-bit apps with VC Express.
EDIT: OpenCV now ships with 64-bit Python binaries. The .dll files need to go somewhere in your path (I put them in the scripts folder), and the .pyd files go in your site-packages directory.
I had trouble interfacing OpenCV with Python, and I was looking all over the place for help. Here's what worked for me. I basically followed this post: http://opencvpython.blogspot.com/2012/05/install-opencv-in-windows-for-python.html. After downloading and extracting OpenCV 2.4.6, you basically get a folder called "opencv" with a bunch of stuff in it. Navigate to build->python->2.7. Inside, there is only one file called "cv2.pyd". I copied this file and pasted it in "python-2.7.5\Lib\site-packages". I'm actually using the Spyder IDE, and it works fine. In the python interpreter, typing in "import cv" worked for me.
Maybe you should edit your environment variable
right click on the "My Computer" or something like this, click on properties.
In the properties window click on the Advanced tab.
Then, the environment variables button.
Change the path.

Categories

Resources