I am trying to load my DLL (32bit) file containing CPP functions into python. It works on python 3.7 (32bit) but it gives an error when using canopy 3.5 (32bit).
the code I use to load my dll:
import os
import ctypes
os.chdir(r"G:\DLLdirectory")
mydll = ctypes.cdll.LoadLibrary('MyDLL.dll')
If I run it on pyton 3.7 it works, if I run it with canopy 3.5 I get:
Traceback (most recent call last):
File "DIR/PythonFile.py", line 26, in <module>
mydll = ctypes.cdll.LoadLibrary('MyDLL.dll')
File "DIR\Canopy32\edm\envs\User\lib\ctypes\__init__.py", line 425, in LoadLibrary
return self._dlltype(name)
File "DIR\Canopy32\edm\envs\User\lib\ctypes\__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
If you change os.chdir() to sys.path.append() in canopy still the module is not found and in python 3.7 I get this error:
Traceback (most recent call last):
File "DIR/PythonFile.py", line 26, in <module>
mydll = ctypes.cdll.LoadLibrary('MyDLL.dll')
File "DIR\Python\Python37-32\lib\ctypes\__init__.py", line 434, in LoadLibrary
return self._dlltype(name)
File "DIR\Python37-32\lib\ctypes\__init__.py", line 356, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
Welcome to StackOverflow!
Not sure whether this is the cause of the error msg that you saw, but FYI the C++ compiler used in building your extension should be the same one used to build the Python that you are using. As you can tell by typing
python, Enthought's Python 3.5.2 is built withVisual C++ 2015 (14.0) 1900See https://stackoverflow.com/a/2676904/1988991It's likely that you built your DLL with a later compiler. Since it seems to work with Python 3.7, Visual Studio 2017 or 2019 seem likely. See https://www.scivision.dev/python-windows-visual-c-14-required/