Pebaz / nimporter

Compile Nim Extensions for Python On Import!
MIT License
824 stars 33 forks source link

Basic example fails on windows with "ImportError: DLL load failed: %1 is not a valid Win32 application" #12

Closed AngelEzquerra closed 4 years ago

AngelEzquerra commented 4 years ago

I just tried to follow the example on the readme file (creating a simple nim_math.nim file and a python nimporter_test.py file to use nim_math). I got the following error on my Windows computer:

python nimporter_test.py Traceback (most recent call last): File "nimporter_test.py", line 2, in import nimporter, nim_math ImportError: DLL load failed: %1 is not a valid Win32 application.

I'm using python 3.7.1 and nim 1.2.0. I installed nimporter using "pip install nimporter". I installed nimpy using "nimble install nimpy".

Pebaz commented 4 years ago

Can you please provide:

I'm sorry this happened, I made sure to test on Windows 10 x86_64 MSVC so I'm sure its related to an architecture mismatch of some sort.

Also, just a side note, the architecture of the Nim compiler, C compiler, and Python have to match (all be x86 or all be x64).

AngelEzquerra commented 4 years ago

I guess the problem might be that my python version is 32 bits while my windows and nim versions are 64 bit:

git hash: 7e83adff84be5d0c401a213eccb61e321a3fb1ff active boot switches: -d:release


- Python version info (x86 or x64, MSVC or GCC, etc.)
`Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32`

If the problem is due to this mismatch it would be nice if the error message could somehow be improved (e.g. it could provide a hint indicating that there might be a 32/64 bit build mismatch between python and the nim generated dll).

I'll try with a 64 bit python and report back.
Pebaz commented 4 years ago

Thank you for providing all these details! I would wager that the 32 vs 64 bit is definitely the issue. I have updated Nimporter to be able to provide this information (if possible) when it cannot import something that it just compiled. So error messages have been improved for exactly this situation. If you are interested, you could pull master and then reimport to see the new error message and then try with 64 bit after to test.

Pebaz commented 4 years ago

Oh, also, I just saw something else. The compiler used to build Python must match the compiler used by Nim. Right now, your Python is MSVC (the MSC in the Python version means MSVC, I have no idea why they didn't just use "MSVC") but your system C compiler is GCC. To use Nim along with Python, you will need to have a C compiler that matches Python (both the architecture (x86 or x64) and the actual compiler (MSVC/Clang/GCC). Sorry it's a bit fiddly, but the cool thing about Nimporter is that when you ship a binary distribution of your library to an end user, they won't need to have a Nim or C compiler installed!

Pebaz commented 4 years ago

I will go ahead and mark this as closed as I am assuming that you were able to get it to work.