Compiling Python Code
July 12, 2003 | Fredrik Lundh
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 McMillan’s installer (cross-platform)
- Thomas Heller’s py2exe (Windows)
- Anthony Tuininga’s cx_Freeze (cross-platform)
- Bob Ippolito’s 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).
Compiling Python modules to byte code #
Python automatically compiles Python source code when you import a module, so the easiest way to create a PYC file is to import it. If you have a module mymodule.py, just do:
>>> import mymodule
to create a mymodule.pyc file in the same directory. A drawback is that it doesn’t only compile the module, it also executes it, which may not be what you want. (however, it does compile the entire script even if it fails to execute the script).
To do this programmatically, and without executing the code, you can use the py_compile module:
import py_compile py_compile.compile("mymodule.py")
There’s 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)
More on byte code #
Python’s byte code is portable between platforms, but not necessarily between Python releases. The imp.get_magic() function returns a 4-byte string identifying the byte code format used by the current interpreter.
You can use the compile function and the marshal module to compile Python code into code objects, and convert such code objects to binary strings. To reverse this process, use marshal to convert from strings to code, and use exec to execute code.
Examples to be added.