Modules obtained using this combination fail to load. I tested the same code against: Visual Studio 2015, Visual Studio 2019 and Mingw-w64 8.1. It fails only in the last case.
>>> import example
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing example: The specified module could not be found.
Closing as this is not related to pybind11 but to the following change in the Python API:
DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory() are searched for load-time dependencies. Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution.
Source: https://docs.python.org/3.8/whatsnew/3.8.html#changes-in-the-python-api
I've met the same problem. I wrote the demo code following this page, and used CMake to build and make the project with MinGW compiler. But I ran into the same problem as follows when testing the demo.
>>> import example
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing example: The specified module could not be found.
I've read the page you mentioned but have no idea about how to handle with this problem. Could you please tell me how to fix?
My platforms' version info are as follows:
Windows 10 Home 1909 (64bits)
Python 3.8.0 [MSC v.1916 32 bit (Intel)] on win32
CMake v3.19.4
gcc (MinGW.org GCC-8.2.0-3) 8.2.0
g++ (MinGW.org GCC-8.2.0-3) 8.2.0
I got the same error and solved with the following link, it's just as Baljak said before.
https://cython.readthedocs.io/en/latest/src/tutorial/appendix.html
Thanks a lot!
It seemed that I've mistook the error info. ImportError: DLL load failed while importing example: The specified module could not be found.
means that some DLLs which example
module needs are missing, instead of example
's missing.
A solution is as follows:
>>> import os
>>> os.add_dll_directory("C:\\MinGW\\bin") # The path where DLLs the customized pybind module needs are locating.
<AddedDllDirectory('C:\\MinGW\\bin')>
>>> import example
>>> example.add(1, 2)
A Dependency Walker is helpful in checking which DLL should be added via os.add_dll_directory
.
I got the same error and solved with the following link, it's just as Baljak said before.
https://cython.readthedocs.io/en/latest/src/tutorial/appendix.html
Very much thanks! Instead of add .dll
folder to os, the more direct and elegant solution is to add -static-libgcc -static-libstdc++
to the compilation command. This will embed the the libstd and libgcc to the pyd
file via static link.