views:

7713

answers:

9

I thought I heard that py2exe was able to do this, but I never figured it out. Has anyone successfully done this? Can I see your setup.py file, and what command line options you used?

Basically I'm thinking of it giving me a single executable file that does something like unzips itself to maybe /temp and runs.

A: 

I believe that you can only get it down to an exe, library.zip, and the requisite dll (and pyd?) files. You could write a little app in C or whatnot that unpacks to temp and runs the exe, though.

Cody Brocious
A: 

No, it's doesn't give you a single executable in the sense that you only have one file afterwards - but you have a directory which contains everything you need for running your program, including an exe file.

I just wrote this setup.py today. You only need to invoke python setup.py py2exe.

Torsten Marek
+22  A: 

PyInstaller will create a single .exe file with no dependencies; use the --onefile option. It does this by packing all the needed shared libs into the executable, and unpacking them before it runs, just as you describe. I don't think py2exe has this feature. (EDIT: apparently it does, see minty's answer)

I use the version of PyInstaller from svn, since the latest release (1.3) is somewhat outdated. It's been working really well for an app which depends on PyQt, PyQwt, numpy, scipy and a few more.

dF
I found py2exe worked a lot better than pyInstaller, when you're using eggs. minty's answer is the solution.
gbjbaanb
A: 

I recently used py2exe to create an executable for post-review for sending reviews to ReviewBoard.

This was the setup.py I used

from distutils.core import setup
import py2exe

setup(console=['post-review'])

It created a directory containing the exe file and the libraries needed. I don't think it is possible to use py2exe to get just a single .exe file. If you need that you will need to first use py2exe and then use some form of installer to make the final executable.

One thing to take care of is that any egg files you use in your application need to be unzipped, otherwise py2exe can't include them. This is covered in the py2exe docs.

David Dibben
+38  A: 

The way to do this using py2exe is to use the bundle_files option in your setup.py file. For a single file you will want to set bundle_files to 1 and set the zipfile option to None. That way it creates one file for easy distribution.

Here is a more complete description of the bundle_file option quoted directly from the py2exe site*

Using "bundle_files" and "zipfile"

An easier (and better) way to create single-file executables is to set bundle_files to 1 or 2, and to set zipfile to None. This approach does not require extracting files to a temporary location, which provides much faster program startup.

Valid values for bundle_files are:

  • 3 (default) don't bundle
  • 2 bundle everything but the Python interpreter
  • 1 bundle everything, including the Python interpreter

If zipfile is set to None, the files will be bundle within the executable instead of library.zip.

Here is a sample setup.py:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1}},
    windows = [{'script': "single.py"}],
    zipfile = None,
)
minty
is it possible to create a single file, that depends on the python dll? I don't mind 1 exe+1 dll (as the dll can be installed to a common location), as long as all the little dependency files are hidden away in the exe.
gbjbaanb
Sure if you want only the interpreter not bundled then choose 2 for the bundle_files option. Good luck!
minty
+2  A: 

As the other poster mention, py2exe, will generate an executable + some libraries to load. You can also have some data to add to your program.

Next step is to use an installer, to package all this into one easy-to-use installable/unistallable program.

I have used InnoSetup ( http://www.jrsoftware.org/isinfo.php ) with delight for several years and for commercial programs, so I heartily recommend it.

Bluebird75
A: 

Minty's answer was very very helpful, I had no idea you could package everything in the executable with only the MSVCR71.dll and w9pxopen.exe left in the directory...I wish I could have found it in the py2exe documentation.

+1  A: 

I'm told bbfreeze will create a single file .EXE, and is newer than py2exe.

Matt
+1  A: 

You should create an installer, as mentioned before. Even though it is also possible to let py2exe bundle everything into a single executable, by setting bundle_files option to 1 and the zipfile keyword argument to None, I don't recommend this for PyGTK applications.

That's because of GTK+ tries to load its data files (locals, themes, etc.) from the directory it was loaded from. So you have to make sure that the directory of your executable contains also the libraries used by GTK+ and the directories lib, share and etc from your installation of GTK+. Otherwise you will get problems running your application on a machine where GTK+ is not installed system-wide.

For more details read my guide to py2exe for PyGTK applications. It also explains how to bundle everything, but GTK+.

Sebastian Noack