Cython pyd files not importing through python

Viewed 2078

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.

2 Answers

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.

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).

Related