0

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
Myriam
  • 1
  • 1

2 Answers2

0

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 with Visual C++ 2015 (14.0) 1900 See https://stackoverflow.com/a/2676904/1988991

It'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/

Jonathan March
  • 5,800
  • 2
  • 14
  • 16
  • Thank you for the suggestion. You are right, the DLL was built in Visual Studio 2017. I tried compiling my DLL in Visual Studio 2015. Unfortunately this did not change anything. The DLL (old and new) still loads in python 3.7 and not in 3.5. – Myriam Jun 25 '19 at 09:39
  • Are you absolutely certain that both your Canopy Python and your compilation are 32-bit? – Jonathan March Jun 25 '19 at 15:05
  • Yes, I checked, they are both 32-bit. We just tried to load the DLL with python 3.5.2. downloaded from the python website and then the DLL loads correctely. So it seems to have something to do with the Canopy shell. – Myriam Jun 28 '19 at 13:57
  • Does MyDLL.dll have any dependencies (i.e. load / reference any other DLLs)? If so, what happens with a dummy MyDLL.dll that has no dependencies? – Jonathan March Jun 28 '19 at 21:49
  • It turned out indeed that the DLL was dependent on another DLL and this DLL was found in python automatically. However, in Canopy the second DLL needed to be loaded separately. @Jonathan March, Thank you for all the help! – Myriam Jul 01 '19 at 06:35
  • Sounds like this suggestion did give you a path to the solution http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Jonathan March Jul 01 '19 at 14:24
0

It turned out that the DLL was dependent on another DLL and this DLL was found in python automatically. However, in Canopy the second DLL needed to be loaded separately.

Myriam
  • 1
  • 1