Cython pyd files not importing through python
Asked Answered
C

2

4

I'm writing a program in python that simulates the reaction of particles, with the aim of teaching the user about particle reactions. As python was too slow at doing the necessary processing, I turned to Cython for speed gains and it worked a treat. I can compile my .pyx file into a .pyd file that can be imported and run from python with a simple import statement (i.e. "import module").

However, the program eventually has to run on another person's computer, and on this computer, the .pyd file will not import. When I try I get this error message:

"ImportError: DLL load failed: The specified module could not be found."

The .pyd file is in exactly the same location on both computers, however, I am running python 3.6 while the other computer has python 3.3 installed. Also, my computer has Cython installed while the other computer does not. Both machines are 32-bit.

I cannot simply compile the entire program to a .exe file as the other computer has a block on .exe files.

I have trawled through stack's questions on Cython, and have also studied the Cython documentation, all to no avail.

Can someone explain to me why the import isn't working, and how I can get it to work? Some extra background on exactly what .pyd files are and how python calls them would also be nice.

EDIT: I run the program from a file called main.py . In the same directory, I have a folder called main, which stores the code I use in modules. main.py calls PageManager.py, which calls ParticleModel.py, which calls MoveParticles.pyd. (These three files are stored in the folder main). I use the statement

"import main.MoveParticles" 

to import the .pyd file, which works on my computer.

Condottiere answered 24/9, 2017 at 15:59 Comment(6)
I've edited my question to provide these details. Unfortunately I can't get images to upload, otherwise I would've shown you the actual files...Condottiere
How does your setup.py look like?Lew
#cython setup file from distutils.core import setup from Cython.Build import cythonize setup(ext_modules = cythonize(MYFILEPATH.pyx)) I compiled my .pyx file, and then moved the resultant .pyd file from my python folder to inside my main folder (referenced before)Condottiere
I don't have much experience with Windows, but your pyd will depend on some runtime. Maybe this runtime is not installed on the other machine or the other python version uses another runtime?Lew
The Compiler Versions are different wiki.python.org/moin/WindowsCompilers and therefore not compatible. But even if the compilers are compatible (Linux) you may run into problems when using different Python Versions. I think the easiest way would be to compile a pyd file for multiple Python versions.Unkenned
I compiled different pyd files for different versions of python 3.x, and none worked until I tried Python 3.5. It appears that the other computer must lack a necessary runtime for Python 3.3 and Python 3.4, but has all the necessary runtimes for Python 3.5. Problem solved (in my case) - thanks!Condottiere
R
1

According to https://cython-devel.python.narkive.com/gqx0VU3L/importerror-dll-load-failed-the-specified-module-could-not-be-found

Just interpreting the error you're describing (ImportError: DLL load failed: could not be found), the dynamic linker couldn't find a library it needed. Most likely this is either a symptom of missing dependencies or a path problem. Here's my suggestions for diagnosing and fixing the problem:

Missing Dependencies: One very simple way to confirm that all the dependencies of your cython module are available is to point the dependency walker utility[1] at it, and look for missing DLLs.

Directory Structure: Is the .pyd file you built from your cython module in the PYTHONPATH (or your current working directory? If it's not, there's your issue.

In case of Missing Dependencies, download dependency walker here: http://www.dependencywalker.com/. Then open your .pyd file and wait a moment. See if there are missing files in the main branchs and add those files to the directory containing python38.dll (my case: C:\Users\PC\AppData\Local\Programs\Python\Python38).

Ruffi answered 4/2, 2021 at 16:50 Comment(0)
C
1

Ok, the difficulty here arose from confusion over which DLL is missing.

I had (naively) assumed that because I was trying to import a .pyd file, which is essentially a DLL, (Python C extension: Use extension PYD or DLL?), that this was causing the error.

However, what actually was causing the error was the lack of a DLL required to run my .pyd file. I resolved the issue by using a different version of python (3.5 to be precise), and after recompiling my .pyd file to work on python 3.5 the application worked a treat.

Summary: the computer attempting to run my file lacked some neccessary DLLs, and hence caused the program to crash.

Condottiere answered 5/11, 2017 at 16:37 Comment(0)
R
1

According to https://cython-devel.python.narkive.com/gqx0VU3L/importerror-dll-load-failed-the-specified-module-could-not-be-found

Just interpreting the error you're describing (ImportError: DLL load failed: could not be found), the dynamic linker couldn't find a library it needed. Most likely this is either a symptom of missing dependencies or a path problem. Here's my suggestions for diagnosing and fixing the problem:

Missing Dependencies: One very simple way to confirm that all the dependencies of your cython module are available is to point the dependency walker utility[1] at it, and look for missing DLLs.

Directory Structure: Is the .pyd file you built from your cython module in the PYTHONPATH (or your current working directory? If it's not, there's your issue.

In case of Missing Dependencies, download dependency walker here: http://www.dependencywalker.com/. Then open your .pyd file and wait a moment. See if there are missing files in the main branchs and add those files to the directory containing python38.dll (my case: C:\Users\PC\AppData\Local\Programs\Python\Python38).

Ruffi answered 4/2, 2021 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.