1

I am trying to import a .dll in Python using the ctypes package.

import ctypes
example_dll = ctypes.windll.LoadLibrary(".\Example.dll")

This .dll has 2 other .dll dependencies which I have installed in the same location. The code runs fine when I use Anaconda (Python 3.7) but as soon as I run it with my Python3.10 it throws the following error:

FileNotFoundError: Could not find module: '...\Example.dll' (or one of its dependencies)> Try using the full path with constructor syntax.

I tried to use the full path, I used dependencyWalker and the dependencies should be there. It does run with Anaconda so it's unclear to me why I can't use it with my normal Python 3.10. The .dll is a 64 bit and my Python3.10 is as well.

Any ideas?

Update

The dll is 64 bit and my python.exe is 64 as well so this should be fine. I checked also all the .dll dependencies using dumpbin and they all exist (and are 64 bit).

Other things I have tried:

(1) Installed 'normal' python 3.7 since my Anaconda distribution uses python 3.7. Does not resolve the error.

(2) Used os.add_dll_directory(), tried full path for my .dll but this does not resolve the error.

(3) Used ctypes.CDLL(), but no success.

I was comparing the CDLL class in ctypes.init.py between my Anaconda distribution and my normal python and saw a small difference with regards to winmode:

Anaconda

        if _os.name == "nt":
        if winmode is not None:
            mode = winmode
        else:
            import nt
            mode = nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
            if '/' in name or '\\' in name:
                self._name = nt._getfullpathname(self._name)
                mode |= nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
            # PATH is how DLL lookup has always worked in the past
            # in Python on Windows. IMHO both the above mode flags
            # are not wanted and cause many serious regressions within
            # the conda ecosystem on Windows. We should however
            # propagate any PATH changes that have happened to Python
            # library and that is not yet implemented.
            LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
            mode = LOAD_WITH_ALTERED_SEARCH_PATH

Python (3.10)

        if _os.name == "nt":
        if winmode is not None:
            mode = winmode
        else:
            import nt
            mode = nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
            if '/' in name or '\\' in name:
                self._name = nt._getfullpathname(self._name)
                mode |= nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR

The last part is missing in my normal python. I tried to add it to and rerun, but then I get the following error:

OSError: [WinError 193] %1 is not a valid Win32 application

Which is related to 32 vs 64 bit. Could this be just a bug in Python? It works with Anaconda but not normal python ...

Dependencies on my Example.dll below. I have blacked out the last 2 .dlls as these are proprietary (these two are stored in the same folder as my Example.dll). When I debug my code I can see the issue is with Example.dll and not with the others.

enter image description here

Siron
  • 11
  • 2
  • The paths in your code and in the error differ. What if `os.add_dll_directory("path_to_dir_where_example_dll_is")`? – CristiFati Jul 20 '23 at 06:29
  • I get the same error when using os.add_dll_directory() – Siron Jul 20 '23 at 11:05
  • Then there are also other *.dll*s involved. Check [\[SO\]: Discover missing module using command-line ("DLL load failed" error) (@CristiFati's answer)](https://stackoverflow.com/a/74883130/4788546). – CristiFati Jul 20 '23 at 13:19
  • I have checked all my .dlls using dumpbin and none of them are missing (I checked this). Also my code works with Anaconda python so if there was a .dll missing it would not work when running with Anaconda (python 3.7) right? The problem is that it's only complaining of missing dependencies when running with normal Python 3.10. – Siron Jul 20 '23 at 13:56
  • Install a "regular" *Python3.7* and see if it reproduces. I still think it's [\[SO\]: PyWin32 and Python 3.8.0 (@CristiFati's answer)](https://stackoverflow.com/a/58632354/4788546), which should be taken care of *os.add\_dll\_directory*: make sure to double the the *BackSlash*es (`"path\\to\\example"`) or use **raw string** (`r"path\to\example"`). – CristiFati Jul 20 '23 at 14:14
  • Thanks, I have tried all these suggestions but no luck so far. Updated my OP. – Siron Jul 21 '23 at 09:22
  • Does it work with full paths? you should probably make the *.dll*s available somewhere. Also show the dependent *.dll*s for all 3 of them. – CristiFati Jul 21 '23 at 09:57
  • I have tried to use full path as well, but that does not resolve it. Unfortunately I can't show any details on the .dlls because its all proprietary (I realise it makes it very difficult to solve here). – Siron Jul 21 '23 at 10:19
  • Maybe one depends on *python3.7*.dll? – CristiFati Jul 21 '23 at 10:37
  • I can not see any dependency on python3.7.dll? I updated my answer with some thoughts and also the dependencies. – Siron Jul 21 '23 at 10:52
  • Even the *.dll* names are secret? :)))) I see some dependencies on **debug** version of *VStudio* stuff. Maybe *Anaconda* ship those? – CristiFati Jul 21 '23 at 11:52

1 Answers1

0
import ctypes

dll_directory = r'C:\path\to\dll_directory'
ctypes.windll.AddDllDirectory(dll_directory)

example_dll = ctypes.WinDLL('Example.dll')

Or:

import ctypes

example_dll = ctypes.CDLL(r'C:\path\to\Example.dll')

Check the DLL dependencies: Use a tool like Dependency Walker or dumpbin (part of Visual Studio) to verify that all the required DLL dependencies are present and compatible with your Python version. Are they all 32-bit or 64-bit?

Also, try running as administrator: In some cases, access restrictions might be causing issues. Try running your Python script as an administrator.

Andrew Arrow
  • 4,248
  • 9
  • 53
  • 80
  • Thanks, I tried both suggestions but I get the same error. I tried before adding full path etc. but none of these options seem to work. – Siron Jul 19 '23 at 14:46
  • ok i updated answer with other ideas. – Andrew Arrow Jul 19 '23 at 14:51
  • Thanks. I tried to run as administrator but it did not help. It's a strange behaviour. I am using PyCharm as IDE and when I use Anaconda as interpreter it runs fine, however if I try to run it from command terminal using Anaconda ( > C:\ProgramData\Anaconda3\python.exe dll_test.py) it also throws an error: OSError: [WinError 193] %1 is not a valid Win32 application. This seems more like a 32 vs 64 bit issue, but my anaconda is 64bit. I will check my dll's. – Siron Jul 19 '23 at 15:14
  • oh this sounds like 32-bit DLL with a 64-bit Python interpreter or vice versa for sure. Install 32-bit Python? Setup VMs with the right bits vs using your real system? – Andrew Arrow Jul 19 '23 at 15:16
  • I tried with a 32 bit Python (I already had one installed) but I then get the [WinError 193] %1 is not a valid Win32 application. The situation is this: my .dll and all its dependencies are 64 bit (I checked this), it does not run with Python 3.10 (64 bit) but it runs with Anaconda\python.exe (also a 64 bit) in Pycharm IDE, but it does not run with Anaconda\python.exe if I run it from command line. – Siron Jul 19 '23 at 15:35
  • hmm i'm out of ideas except 1 more thing to try: os.chdir(dll_directory) – Andrew Arrow Jul 19 '23 at 16:16