18

When trying to use py2exe to convert a simple Python game I made into exe format, it gave me the following error:

Traceback (most recent call last):
  File "C:\Users\Tali\Desktop\2exe.py", line 4, in <module>
    setup(console=['test.py'])
  File "C:\Python\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python\lib\site-packages\py2exe\build_exe.py", line 243, in run
    self._run()
  File "C:\Python\lib\site-packages\py2exe\build_exe.py", line 305, in _run
    dlls = self.find_dlls(extensions)
  File "C:\Python\lib\site-packages\py2exe\build_exe.py", line 389, in find_dlls

    self.dll_excludes)
  File "C:\Python\lib\site-packages\py2exe\build_exe.py", line 1021, in find_dep
endend_dlls
    import py2exe_util
ImportError: DLL load failed: %1 is not a valid Win32 application.

I searched the web for about an hour, tried changing things in my program. Nothing works. Although my program uses the "random" library, I haven't seen a case in which it caused this kind of problem.

The install script:

from distutils.core import setup
import py2exe

setup(console=['test.py'])

Yes, both the scripts are in the same folder (Desktop). If it matters, I'm running on Windows Vista with Python 2.7

Thank you all very much in advance.

Niv
  • 379
  • 2
  • 3
  • 8
  • Can you check, if you are not having an issue of 64 bit/ 32 bit incompatibility. – pyfunc Sep 23 '10 at 20:12
  • 1
    I have a Vista 32-bit, and an not sure how I'm supposed to check for such incompatibility. – Niv Sep 23 '10 at 20:24
  • Try running dependency walker on the resulting executable to see if you need to ship some extra dependencies... – ChristopheD Sep 23 '10 at 20:28
  • There is no resulting exe. The error occurred when trying to turn the Python into an exe. And yes, I double-checked. – Niv Sep 23 '10 at 20:32
  • How are you executing the script? normally the setup file is called setup.py (not 2exe.py) and must be called as 'python setup.py py2exe' – joaquin Sep 23 '10 at 21:43
  • 1)Command Prompt, type cd Desktop. 2)type setup.py py2exe (after I changed the name to setup) 3)Wait for the text to roll, and get the error. When I try to use with a 'python' prefix, I get a "python is not a recognizable..." error – Niv Sep 23 '10 at 22:08
  • If you can not execute python from the command line then you have a problem. Is python.exe in your PATH?. If you type 'python' you should get the python shell... There are other program exe's you can not execute and get the windows error?. Which distribution of python did you installed? – joaquin Sep 23 '10 at 22:33
  • I'm not sure which path you refer to. I haven't had any problem with any other program, and I've installed it via the Python website: http://python.org/ftp/python/2.7/ . Python is installed directly on C if it makes any difference. – Niv Sep 23 '10 at 22:45
  • in xp you can check/modify your PATH in Control Panel->System->Advanced Options->Environment vars then in 'system vars' you should have c:/Python27 in the list of paths in the path variable. Alternatively you can check your PATH in the windows shell (C:\>PATH) – joaquin Sep 23 '10 at 22:56
  • Is your app a console-based app? Can you give more info about your app – luc Sep 24 '10 at 05:40
  • @joaquin - I tried adding a Python system-variable path via. the control panel that leads directly to python.exe. Even after a restart, no effect. @luc - Nothing serious. Just a short guess-the-number text game I made. – Niv Sep 24 '10 at 09:56
  • do not use py2exe the last stable release is ten years old (2008). – guettli Aug 07 '18 at 10:35

9 Answers9

33

I had exact the same problem.

Since I have windows 7 64bit, I downloaded py2exe-0.6.9.win64-py2.6.amd64.exe, which I suppose to be the 64bit version of py2exe. but it did not work, and I had the same error.

I changed to py2exe-0.6.9.win32-py2.6.exe, and it worked fine.

I guess you have to match the 32bit or 64bit with the python installation instead of the windows itself.

by the way, "setup.py py2exe" or "python setup.py py2exe" really does not matter, if you instructed windows to open *.py with python during the python installation.

xiao-yu
  • 331
  • 2
  • 3
  • You can definitely run into problems if you assume that "setup.py" is identical to "python setup.py". It's not uncommon to have more than one Python installation on the same machine (v2.x and perhaps both 32-bit and 64-bit variants of v3.x). If you switch between the various Python versions using a CMD script that sets PATH and PYTHONHOME environment variables then "python setup.py" will run whichever python.exe is first in your PATH but running "setup.py" will run whichever version of Python you installed last because it is associated with the PY extension. So be careful. – jarmod May 30 '12 at 17:55
3

It seems the problem can be solved by downloading the correct py2exe installer for your platform and version.

I had the same problem here too. I was running on Windows 7 64 bit and had installed Python 2.7. My problem had to do with the fact that I installed py2exe-0.6.9.win32-py2.7.exe and the installer was not able to find python, even though it found the installation folder. Running python setup.py py2exe at this stage gave me the exact same errors you're seeing.

I then downloaded py2exe-0.6.9.win64-py2.7.amd64.exe from this page and installed it. The installation went off without a hitch and python setup.py py2exe game me lots of console output, created the build and dist folders and the expected executable.

Carl
  • 43,122
  • 10
  • 80
  • 104
3

I met the same problem and I searched and I came here . I tried the way prompt by xiao-yu but I didn't work. So I found this from http://www.py2exe.org/index.cgi/Tutorial#Step52: 5.2.1. Bundling the C runtime DLL

If you do have the rights to redistribute MSVCR90.dll, there should be a copy of it in your Visual Studio install, under VC\redist\x86\Microsoft.VC90.CRT. Since Visual Studio 2008, you can't just copy this DLL file - you also need the manifest file that you'll find there. The redist.txt file states that you must distribute all three dlls and the unmodified manifest file and it is a violation of the license agreement to distribute only one of the dlls without the others (though py2exe only needs MSVCR90.dll.) The pertinent passage from the redist.txt file is as follows:-----------------------

so ,try it.

katero
  • 31
  • 3
2

I solved this problem by downloading "Microsoft Visual C++ Compiler for Python 2.7" http://www.microsoft.com/en-pk/download/details.aspx?id=44266

Bilal Mahmood
  • 169
  • 1
  • 3
2

Try installing the Microsoft Visual C++ 2008 Redistributable Package (x86): http://www.microsoft.com/downloads/en/details.aspx?familyid=9B2DA534-3E03-4391-8A4D-074B9F2BC1BF&displaylang=en

Victor Ionescu
  • 1,967
  • 2
  • 21
  • 24
  • This actually fixed it for me. I had a problem with compiling 32bit Python py2exe on a 64bit machine, with python-mysql installed. I got the DLL error. But downloading the x86 Visual C++ 2008 runtime and repairing my installed version worked. – user1479931 Aug 07 '13 at 08:35
0

From your comments, I see that you are doing this in the command prompt:

setup.py py2exe 

from the py2exe tutorial's third step, you should be doing this instead (in the command promt):

python setup.py py2exe

A second look at the comments on the original question shows that this suggestion has already been made. +1 to joaquin to for this.

To try to answer your question again: Joaquin also mentioned that this might be a path issue. Rather than trying to fix that, you might want to consider the easier option of copying your python files into C:\Python. Then in the command prompt:

cd C:\Python
python setup.py py2exe

This has always worked for me

Hope this helps

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • This was already indicated in the comments. The op is not able of getting just 'python' producing the python shell. – joaquin Sep 23 '10 at 23:01
  • I think it's a bad idea to copy Python source files to the Python installation directory. Instead, you should run the desired Python EXE explicitly, for example "c:\python\python setup.py py2exe". Or you should change drive/directory into c:\python (cd /d c:\python) and then run "python d:\somefolder\setup.py py2exe". – jarmod May 30 '12 at 18:00
0

You can deal with py2exe and the headaches, I tried doing this, even when I had it right, it would error, though I did find the excellent python program gui2exe, this makes it extremely easy to make an exe (or any of the other supported formats).

Gui2exe: http://code.google.com/p/gui2exe/downloads/detail?name=GUI2Exe_0.5.0.zip

Examples: http://code.google.com/p/gui2exe/wiki/GUI2ExeExamples

Zimm3r
  • 3,369
  • 5
  • 35
  • 53
  • 1
    @niv ya it does, its just a nice gui that seems to solve some problems magically and also makes it easier (ok maybe not magic but my dll problems went away after using it) – Zimm3r Sep 24 '10 at 19:43
  • I did this and it gave the exact same stacktrace. It's just a frontend for the same non-working software. – korona Mar 04 '11 at 10:38
0

I was facing the same problem, I was using Python 2.7 (32 bits) on Windows 7 (64 bit) with py2exe-0.6.9.win64-py2.7.amd64.exe, so the problem was I was using 32 bit Python and 64 bit py2exe. If you don't know whether Python package you are using is 32 bit or 64 bit, then run idle.bat from C:\Python27\Lib\idlelib\.

The first line in python window prompt will show you the python package details.

I uninstalled the py2exe-0.6.9.win64-py2.7.exe by running "pip uninstall py2exe" and installed py2exe-0.6.9.win64-py2.7.amd64.exe which worked for me.

danglingpointer
  • 4,708
  • 3
  • 24
  • 42
0

py2exe looks very dated. The last release is four years old (from 2014, today is 2018)

The latest entry on the homepage part "news" is from 2008 (ten years old).

I guess it is better to use a different tool if you have errors with py2exe today.

guettli
  • 25,042
  • 81
  • 346
  • 663