Question

I've been trying to get a small aplication I wrote in Python to work as a standalone program in any computer running Windows, so I tried to do so using both cx_freeze and py2exe. Py2exe works fine and dandy but I really prefered using cx_freeze because of some compatibility issues.

The problem with cx_freeze, however, is that after compiling the code and all its dependencies, I can't change the executable's name (which is perfectly doable with py2exe).

So, say I have a simple hello.py script:

print ("Hello World! ")
raw_input ("Press any key to exit. \n")

and my cxfreeze_setup.py, which I copied directly from their website for debugging purposes, looks something like this:

import sys
from cx_Freeze import setup, Executable


setup(  name = "hello",
        version = "0.1",
        description = "My simple hello world!!",
        executables = [Executable("hello.py")])

When I build the standalone calling python cxfreeze_setup.py build in the command prompt, everything goes well and as expected, and the executable plus its dependencies are created in the usual build folder.

If I don't do any name changes to the hello.exe created and run it then everything runs perfectly aswell!

However, say I change the hello.exe to hey.exe. Now, when I try to run hey.exe I get the following error:

Traceback (most recent call last):
File "c:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26 in <module>
code = importer.get_code(moduleName)
zipimport.ZipImportError: can't find module 'hey__main__'

If I change the .exe name to hi.exe then the error stays exactly same except for the last line where it now says can't find module 'hi__main__'

Finally, I was wondering if, with cx_freeze, I'm forced to not change the executable name after compiling it and, if that's not the case, what modifications to either my hello.py or cxfreeze_setup.py scripts must I perform in order to freely modify the executable name after being compiled, something I can perfectly do with py2exe.

Thanks in advance for any help.

Était-ce utile?

La solution 2

Reposting as an answer:

The exe cx_Freeze makes uses its own name to look up the Python script to run. The advatage of this is that you can have multiple exes sharing a set of libraries. The downside is that you can't easily rename exes.

If you do need to rename the exe, open up library.zip, and rename hello__main__.pyc to hey__main__.pyc (the first bit should match your exe's name).

Autres conseils

Use --target-name=NAME, Quote from doc:

--target-name=NAME
the name of the file to create instead of the base name of the script and the extension of the base binary

Or just:

setup(name = "guifoo",
      version = "0.1",
      description = "My GUI application!",
      options = {"build_exe": build_exe_options},
      executables = [Executable("guifoo.py", base=base, targetName="what_you_want.exe")])
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top