Freezing Your Code

https://farm5.staticflickr.com/4227/33907151034_e0a9e53402_k_d.jpg

“Freezing” your code is creating a single-file executable file to distributeto end-users, that contains all of your application code as well as thePython interpreter.

Applications such as ‘Dropbox’, ‘Eve Online’, ‘Civilization IV’, andBitTorrent clients do this.

The advantage of distributing this way is that your application will “just work”,even if the user doesn’t already have the required version of Python (or any)installed. On Windows, and even on many Linux distributions and OS X, the rightversion of Python will not already be installed.

Besides, end-user software should always be in an executable format. Filesending in.py are for software engineers and system administrators.

One disadvantage of freezing is that it will increase the size of yourdistribution by about 2–12MB. Also, you will be responsible for shippingupdated versions of your application when security vulnerabilities toPython are patched.

Alternatives to Freezing

Packaging your code is for distributinglibraries or tools to other developers.

On Linux, an alternative to freezing is tocreate a Linux distro package(e.g. .deb files for Debian or Ubuntu, or .rpm files for Red Hat and SuSE.)

Todo

Fill in “Freezing Your Code” stub

Comparison of Freezing Tools

Solutions and platforms/features supported:

SolutionWindowsLinuxOS XPython 3LicenseOne-file modeZipfile importEggspkg_resources support
bbFreezeyesyesyesnoMITnoyesyesyes
py2exeyesnonoyesMITyesyesnono
pyInstalleryesyesyesyesGPLyesnoyesno
cx_FreezeyesyesyesyesPSFnoyesyesno
py2appnonoyesyesMITnoyesyesyes

Note

Freezing Python code on Linux into a Windows executable was only oncesupported in PyInstallerand later dropped..

Note

All solutions need MS Visual C++ dll to be installed on target machine, except py2app.Only Pyinstaller makes self-executable exe that bundles the dll whenpassing--onefile toConfigure.py.

Windows

bbFreeze

Prerequisite is to installPython, Setuptools and pywin32 dependency on Windows.

  1. Installbbfreeze:
$ pip install bbfreeze
  1. Write most basicbb_setup.py
frombbfreezeimportFreezerfreezer=Freezer(distdir='dist')freezer.addScript('foobar.py',gui_only=True)freezer()

Note

This will work for the most basic one file scripts. For more advanced freezing you will have to provideinclude and exclude paths like so

freezer=Freezer(distdir='dist',includes=['my_code'],excludes=['docs'])
  1. (Optionally) include icon
freezer.setIcon('my_awesome_icon.ico')

4. Provide the Microsoft Visual C runtime DLL for the freezer. It might be possible to append yoursys.pathwith Microsoft Visual Studio path but I find it easier to dropmsvcp90.dll in the same folder where your scriptresides.

  1. Freeze!
$ python bb_setup.py

py2exe

Prerequisite is to installPython on Windows.

  1. Download and installhttp://sourceforge.net/projects/py2exe/files/py2exe/
  2. Writesetup.py (List of configuration options):
fromdistutils.coreimportsetupimportpy2exesetup(windows=[{'script':'foobar.py'}],)
  1. (Optionally)include icon
  2. (Optionally)one-file mode
  3. Generate.exe intodist directory:
$ python setup.py py2exe
  1. Provide the Microsoft Visual C runtime DLL. Two options:globally install dll on target machine ordistribute dll alongside with .exe.

OS X

py2app

PyInstaller

PyInstaller can be used to build Unix executables and windowed apps on Mac OS X 10.6 (Snow Leopard) or newer.

To install PyInstaller, use pip:

$ pip install pyinstaller

To create a standard Unix executable, from sayscript.py, use:

$ pyinstaller script.py

This creates,

  • ascript.spec file, analogous to amake file
  • abuild folder, that holds some log files
  • adist folder, that holds the main executablescript, and some dependent Python libraries,

all in the same folder asscript.py. PyInstaller puts all the Python libraries used inscript.py into thedist folder, so when distributing the executable, distribute the wholedist folder.

Thescript.spec file can be edited tocustomise the build, with options such as

  • bundling data files with the executable
  • including run-time libraries (.dll or.so files) that PyInstaller can’t infer automatically
  • adding Python run-time options to the executable,

Nowscript.spec can be run withpyinstaller (instead of usingscript.py again):

$ pyinstaller script.spec

To create a standalone windowed OS X application, use the--windowed option

$ pyinstaller --windowed script.spec

This creates ascript.app in thedist folder. Make sure to use GUI packages in your Python code, likePyQt orPySide, to control the graphical parts of the app.

There are several options inscript.spec related to Mac OS X app bundleshere. For example, to specify an icon for the app, use theicon=\path\to\icon.icns option.

Linux

bbFreeze

PyInstaller