I have been trying to use cx_freeze for some time now, and yet the same error always occurs
import cx_Freeze.util
ImportError: DLL load failed: %1 is not a valid win32 application
I have tried with both 32 and 64 bit, and have tried both setup.py and cxfreeze.bat
what I am asking is, how do I make it work?
I have had the same problem and this was my solution.
I am using Python 3.2 for windows 64 bit and my library installations were all for 64 bit, including cx_freeze. When I tried to "freeze" a program that imported the PyQt4 package, I got the error:
ImportError: DLL load failed: %1 is not a valid win32 application
In my case, the problem was that the cxfreeze script was copying two DLL files (QtCore4.dll and QtGui4.dll) from my MATLAB installation, rather than from my Python installation. The MATLAB DLLs were 32 bit whereas the Python DLLs were the required 64 bit files.
Read the messages displayed when you run the cxfreeze script to build your exe files. The last few messages should be "copying ...dll-> ....dll". Check the filenames and make sure that all the files are actually being copied from your Python installation and not some other program.
If they are being copied from elsewhere (as in my case) you can simply copy them over yourself after the build. Otherwise, you will have to change your path not to include the folder containing the 32 bit DLLs from whatever program they belong.
Related
I came across both issues these days when working on Python and DLL files. What's the difference between these two ImportErrors?
From my research, I figured out part of the answers to this question.
The first one, DLL load failed: %1 is not a valid Win32 application is probably due to the mix up of the bit version 64 and bit version 32. As such, to my understanding, if I am running a code compiled in Python 2 in Python 3, the problem will happen. The solution to this issue is to use the consistent bit version.
Another one, DLL load failed: The specified module could not be found, is probably caused by:
(1) Inconsistent compile version: the code is compiled with a different compiler (or compiler version) than the binary file that the respective Python module consists of (reference). In my case, my code was compiled in Python 3.4, and the error raised when I imported the module in Python 3.6.
(2) The path to the file is misconfigured (reference)
(3) Some files or "dependencies" are missing (reference). For example, need to install the Visual C++ Redistributable for Visual Studio 2015 in some cases.
Please leave your feedback if I was wrong or the answer can be improved. Thank you!
I am running py2exe to convert a rather large (~20k lines) python file into an executable. It's an older program and is written in python 2.7, 32 bit, so it does not translate well to a minimum working example. On running py2exe I encounter the following error:
error: [Errno 2] No such file or directory:
'libopenblas.UWVN3XTD2LSS7SFIFK6TIQ5GONFDBJKU.gfortran-win32.dll'
Having this dll in the Python27/DLLs folder will solve this issue, but after scouring the depths of google I have been unable to make any progress. My question: where can one download this dll, or a library which would contain this dll?
Problem solved. If you're running 32 bit python and numpy, you should be able to find this specific DLL at:
C:\Python27\Lib\site-packages\numpy\.libs
Copy and paste this into:
C:\Python27\DLLs
And run py2exe as normal.
Adding to Dr.Toboggan's answer, you can probably just import numpy inside the setup.py file for p2exe, this is what worked for me on a 64-bit system...
I'm trying to follow the instructions here to use the Boost.Python. The source code is in that webpage. I can compile, link this simple sample code but I cannot import the resulting module in python command line. It always error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named hello_ext
I have no idea what the matter is because that page just says: "That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python." This is my building environment:
Windows 7 64 bit, I am the Administrator and run cmd as Administrator
boost version is 1.64.0 (precompiled binary boost_1_64_0-msvc-14.0-64.exe downloaded from here)
python version is 2.7.13, 64 bit
Visual studio 2015, Update 3
The target is a DLL
The project name is ConsoleApplication1, so the output is ConsoleApplication1.dll. I have changed the filename to hello_ext.dll but same error.
I built with x64 configuration and I have verified with dumpbin that the output ConsoleApplication1.dll is really 64 bit
I have added the path ......\ConsoleApplication1\x64\Release which contains ConsoleApplication1.dll into sys.path inside python command line.
So, could you please tell me how to import the module in python? Thanks a lot.
I solved the problem myself. Thank jagerman for his useful suggestions.
(1) Just change the output filename from ConsoleApplication1.dll to hello_ext.pyd. You can automate this rename by setting Pages->General->Target Extension to ".pyd". Make sure the file hello_ext.pyd is in python's search path. You can just throw it to C:\Python27\DLLs which is one of python's built-in search paths.
(2) Now you will got a different import error: DLL load failed: The specified module could not be found. If you look closely at the file size of hello_ext.pyd, you'll likely notice something wired -- it's only 19KB. That means it doesn't contain everything needed to import into python, so python has to find the missing part to properly import it. Yes, you may guess that -- the only possible missing stuff is Boost.Python libraries, so add path to it into PATH environment variable -- for me, it is C:\local\boost_1_64_0\lib64-msvc-14.0.
Then the problem is solved. Note: some answers in other related questions may suggest build as a static library, That way, you will got another import error: DLL load failed: %1 is not a valid Win32 application. So just build as DLL. PS: you don't need to specify boost_python-vc140-mt-1_64.lib or boost_python-vc140-mt-gd-1_64.lib in Property Pages->Linker->Input->Additional Dependencies as some comments suggested.
When importing a DLL module into Python, sometimes I will get:
ImportError: DLL load failed: The specified module could not be found.
The problem is often:
Missing DLL dependency (commonly a Visual Studio runtime DLL)
DLL or dependency compiled for 32 bit when should be 64 bit
Dependency walker answers this question sometimes. But, sometimes it doesn't, particularly if the issue is a missing Visual Studio runtime DLL.
Is there a way to make Python tell me the name of the missing DLL dependency?
Here's what I ended up doing.
I was unable to solve my problem using Python alone. So I used C++ instead.
I made a small win32 application to link with the .lib file produced alongside the .pyd file, and forced it to use a symbol from the .pyd file:
extern "C" __declspec(dllimport) void initmymodule();
...
printf("%p\n", initmymodule);
Then, when running this program, Windows would tell me which DLL was missing.
Yes. Run the script_name.py from the shell, (cmd.exe or powershell in Windows) and read the error message(s). The error will show which dependency is missing.
You may need to repeat this process several times, if you are missing more than one.
Any IDE, including IDLE will display an error message in a window, buried in the error message is the name of the missing file.
I have a python application and I want to extract the executable. I have used py2exe with no problem in other scripts. But in this one I get errors that I think have to do with the fact that I import and use scipy.
My code runs fine in eclipse. Trying to make the procedure with scipy at first I got 3 dlls missing ("libmmd.dll","MSVCP90.dll","libifcoremd.dll"). At first I exluded these from setup.py and the .exe file was made but it did not run. After that I downloaded the dll files (also deleted the exclusion of setup.py) and added them in "C:\Python27\DLLs" and also "C:\Windows\System32" and "C:\Windows\SysWOW64" (I have 64 bit windows7). Again the exe is created in the folder dist, but trying to run it I get this:
http://imageshack.us/photo/my-images/145/py2exe.png/
Then I tried to do it with cx_Freeze. Again the .exe file was created but I get this error:
http://img521.imageshack.us/img521/3843/cxfreeze.png
So I suppose it has nothing to do with py2exe or cx_Freeze, but with the fact that scipy needs some dependencies that for some reason work in eclipse, but not when I make the executable.
Has anybody faced this problem? Any ideas on what I should do?
Thank you in advance!
EDIT: I did it with pyinstaller-2.0. It did not run at first. Then I added the DLLs to the folder, after that I added numpy and scipy to the folder and finally after an error message I added kdtree.py to the folder. I was thrilled to see that it runned! But then dissapointed again cause it runs ONLY in my pc (some useful .exe there)! I added the folder it to a dropbox folder, from where it runs in me with no problem but not for others. The error in other machines I tried is:
importError: DLL load failed: %1 is not a valid Win32 application
You could try putting the DLL files directly next to your executable. Also my py2exe gives some information about possible binary files that you might need to distribute with your application:
*** binary dependencies ***
Your executable(s) also depend on these dlls which are not included,
you may or may not need to distribute them.
Make sure you have the license if you distribute any of them, and
make sure you don't distribute files belonging to the operating system.
//Snip list of paths to dlls