Compiling Python Code
Compiling Python Code
Compiling Python Code
Python source code is automatically compiled into Python byte code by the CPython
interpreter. Compiled code is usually stored in PYC (or PYO) files, and is regenerated
when the source is updated, or when otherwise necessary.
To distribute a program to people who already have Python installed, you can ship
either the PY files or the PYC files. In recent versions, you can also create a ZIP archive
containing PY or PYC files, and use a small bootstrap script to add that ZIP archive to
the path.
To compile a Python program into an executable, use a bundling tool, such as:
Gordon McMillans installer (cross-platform)
Thomas Hellers py2exe (Windows)
Anthony Tuiningas cx_Freeze (cross-platform)
Bob Ippolitos py2app (Mac)
These tools puts your modules and data files in an archive file, and creates an
executable that automatically sets things up so that modules are imported from that
archive. Some tools can embed the archive in the executable itself.
If all you need is to wrap up a couple of Python scripts and modules into a single file,
Squeeze might be what you need. For Windows, my ExeMaker tool can also be quite
useful (on its own, or in combination with squeeze).
Theres also a compileall module which can be used to compile all modules in an
entire directory tree.
import compileall
compileall.compile_dir("mylib", force=1)
1/2
9/7/2014
http://effbot.org/zone/python-compile.htm
2/2