0

I have a python module as a mymodule.dll, and I would like to load it without having it in sys.path. I can do this using importlib:

mymodule = importlib.machinery.ExtensionFileLoader(
    "mymodule", r"C:\X\Y\mymodule.dll").load_module()

Now, mymodule.dll depends on other DLLs, and I have issues when trying to load it. If I put all the DLLs the module depends on next to it e.g.:

C:\X\Y
    mymodule.dll
    Qt5Core.dll

Then, I can import mymodule.dll using the above method from anywhere. But if I move Qt5Core.dll elsewhere, it always fail with:

ImportError: DLL load failed while importing mymodule: The specified module could not be found.

...even if Qt5Core.dll is in my PATH (and ctypes.util.find_library finds it).

Is there a way to load mymodule.dll without having to copy its dependencies in the same folder?

Holt
  • 32,271
  • 6
  • 74
  • 116
  • What's the *Python* version? Note: as a convention, extension modules are named *.pyd*. You get the behavior on the machine you built your *.dll* (I assume you have *VStudio* installed there) or on another one? – CristiFati May 13 '20 at 09:55
  • @CristiFati Python version 3.8 (same behavior with 3.7 I think). Behavior on the machine I built the `.dll`. I "solved" the issue by loading all the dependencies manually using `ctypes.cdll.LoadLibrary`, but I am very curious as to why these were not loaded automatically. I know that modules are named `.pyd`, but I have a `.dll` because the module is actually meant to be loaded in C++ via `boost::python` (this is an API to allow extending a C++ application with Python). The module is not meant to be actually loaded in python, I just need to load it to generate proper stubs for it. – Holt May 13 '20 at 09:58
  • Is this what you're encountering? [\[SO\]: PyWin32 and Python 3.8.0 (@CristiFati's answer)](https://stackoverflow.com/questions/58631512/pywin32-and-python-3-8-0/58632354#58632354). – CristiFati May 13 '20 at 10:03
  • @CristiFati I am going to check but basically if it is the same issue, it should work with python 3.7 right? – Holt May 13 '20 at 10:31
  • That's correct. – CristiFati May 13 '20 at 10:34
  • @CristiFati Ok, so this seems to be the same issue. With 3.7, everything works fine if I add the relevant folders to `PATH`, with 3.8 it does not work but if I use `os.add_dll_directory`, it works. Thanks for pointing that out, I am going to close as dupe! – Holt May 13 '20 at 10:37
  • Glad to hear it worked out! Sure, go ahead and mark it as such. – CristiFati May 13 '20 at 11:10

0 Answers0