Create a single standalone .exe from a Python program

I have been working on a small command line tool that I wanted to distribute it as a single executable file on . I tried cx_freeze and py2exe. Both of these tools worked well, but I couldn’t find an easy way to compress make the whole program into a .exe file. py2exe and cx_freeze both create working programs, but there are always some dependent .zip archive or .dll’s somewhere that need to be distributed with it. Pyinstaller, I found, actually compresses everything into a single .exe. This makes a pretty big executable (my small command line utility created a 5MB .exe file), but it’s simple and it works.

To use pyinstaller:

  1. grab pyinstaller 1.5rc (1.4 doesn’t work with 2.7). extract the zip file anywhere.
  2. change directories to the pyinstaller folder you just created.
  3. Before you create your first executable, you will have to run this once.
  4. python .py
  5. Now, pyinstall needs to scan through your program and create what they call a spec file.
  6. python makespec.py --onefile path\to\program\program.py
  7. Now, run this command to generate the executable.
  8. python build.py program\program.spec

Once the command has finished, the standalone executable will be available in the program\dist folder inside of pyinstaller.
Instructions for how to do this for a linux executable on ubuntu linux can be found here: http://excid3.com/blog/2009/12/pyinstaller-a-simple-tutorial/. You can find more info on pyinstaller at their website: http://www.pyinstaller.org/.

    • Happy
    • August 4th, 2011

    If your purpose of having a single executable is to ease downloading/emailing, etc., I’ve solved this by bundling the py2exe output using Inno Setup. This is actually better than having a single executable, because rather than providing an executable file that can be dropped into some directory, a well behaved Windows application will provide an uninstaller, show up in the Add/Remove Programs applet, etc. Inno handles all this for you.

    • That sounds like a great solution!. I’d love to hear more about your experience going through the process of packaging a python app for windows.

      The purpose of creating this project as a standalone .exe is that for tiny utilities (which is what i was doing for this project), it’s a lot easier to just grab the .exe, use it once and delete it. I will b sharing that project in post in the near future.

  1. No trackbacks yet.