Freezing Your Code
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 are for software engineers and system administrators.
One disadvantage of freezing is that it will increase the size of yourdistribution by about 2–12 MB. Also, you will be responsible for shippingupdated versions of your application when security vulnerabilities toPython are patched.
Packaging your code is for distributinglibraries or tools to other developers.
On Linux, an alternative to freezing is to(e.g. .deb files for Debian or Ubuntu, or .rpm files for Red Hat and SuSE.)
Todo
Fill in “Freezing Your Code” stub
Note
Freezing Python code on Linux into a Windows executable was only oncesupported in PyInstaller and later dropped.
Note
All solutions need a Microsoft Visual C++ to be installed on the target machine, except py2app.Only PyInstaller makes a self-executable exe that bundles the appropriate DLL whenpassing —onefile
to Configure.py
.
Prerequisite is to install .
- Install
bbfreeze
:
- Write most basic
bb_setup.py
- from bbfreeze import Freezer
- freezer = 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'])
- (Optionally) include icon
- Provide the Microsoft Visual C++ runtime DLL for the freezer. It might be possible to append your
sys.path
with the Microsoft Visual Studio path but I find it easier to dropmsvcp90.dll
in the same folder where your scriptresides.
- Freeze!
- $ python bb_setup.py
py2exe
Prerequisite is to install . The last release of py2exe is from the year 2014. There is not active development.
- Download and install http://sourceforge.net/projects/py2exe/files/py2exe/
- Write
setup.py
():
- from distutils.core import setup
- import py2exe
- setup(
- windows=[{'script': 'foobar.py'}],
- (Optionally) include icon
- (Optionally)
- Generate
.exe
intodist
directory:
- Provide the Microsoft Visual C++ runtime DLL. Two options: globally install dll on target machine or .
py2app
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 say , use:
- $ pyinstaller script.py
This creates:
- a
script.spec
file, analogous to amake
file - a
build
folder, that holds some log files - a
dist
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.
The file can be edited to , 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):
To create a standalone windowed OS X application, use the —windowed
option:
- $ pyinstaller --windowed script.spec
This creates a script.app
in the dist
folder. Make sure to use GUI packages in your Python code, like PyQt or , to control the graphical parts of the app.
There are several options in script.spec
related to Mac OS X app bundles here. For example, to specify an icon for the app, use the option.