PyMOTW: compileall

By Doug Hellmann
January 18, 2009

compileall – Byte-compile Source Files

Purpose:Convert source files to byte-compiled version.
Python Version:1.4

The compileall module finds Python source files and compiles them to the byte-code representation, saving the results in .pyc or .pyo files.

Compiling One Directory

compile_dir() is used to recursively scan a directory and byte-compile the files within it.

import compileall

compileall.compile_dir('examples')

By default, all of the subdirectories are scanned to a depth of 10. When using a version control system such as subversion, this can lead to unnecessary scanning, as seen here:

$ python compileall_compile_dir.py
Listing examples ...
Listing examples/.svn ...
Listing examples/.svn/prop-base ...
Listing examples/.svn/props ...
Listing examples/.svn/text-base ...
Listing examples/.svn/tmp ...
Listing examples/.svn/tmp/prop-base ...
Listing examples/.svn/tmp/props ...
Listing examples/.svn/tmp/text-base ...
Compiling examples/a.py ...
Listing examples/subdir ...
Listing examples/subdir/.svn ...
Listing examples/subdir/.svn/prop-base ...
Listing examples/subdir/.svn/props ...
Listing examples/subdir/.svn/text-base ...
Listing examples/subdir/.svn/tmp ...
Listing examples/subdir/.svn/tmp/prop-base ...
Listing examples/subdir/.svn/tmp/props ...
Listing examples/subdir/.svn/tmp/text-base ...
Compiling examples/subdir/b.py ...

To filter directories out, use the rx argument to provide a regular expression to match the names to exclude.

import compileall
import re

compileall.compile_dir('examples',
rx=re.compile(r'/\.svn'))

$ python compileall_exclude_dirs.py
Listing examples ...
Compiling examples/a.py ...
Listing examples/subdir ...
Compiling examples/subdir/b.py ...

The maxlevels argument controls the depth of recursion. For example, to avoid recursion entirely pass 0.

import compileall
import re

compileall.compile_dir('examples',
maxlevels=0,
rx=re.compile(r'/\.svn'))

$ python compileall_recursion_depth.py
Listing examples ...
Compiling examples/a.py ...

Compiling sys.path

All of the Python source files found in sys.path can be compiled with a single call to compile_path().

import compileall
import sys

sys.path[:] = ['examples', 'notthere']
print 'sys.path =', sys.path
compileall.compile_path()

This example replaces the default contents of sys.path to avoid permission errors while running the script, but still illustrates the default behavior. Note that the maxlevels value defaults to 0.

$ python compileall_path.py
sys.path = ['examples', 'notthere']
Listing examples ...
Compiling examples/a.py ...
Listing notthere ...
Can't list notthere

From the Command Line

It is also possible to invoke compileall from the command line, as you might when integrating it with a build system via a Makefile. For example:

$ python -m compileall -h
option -h not recognized
usage: python compileall.py [-l] [-f] [-q] [-d destdir] [-x regexp] [directory ...]
-l: don't recurse down
-f: force rebuild even if timestamps are up-to-date
-q: quiet operation
-d destdir: purported directory name for error messages
if no directory arguments, -l sys.path is assumed
-x regexp: skip files matching the regular expression regexp
the regexp is search for in the full path of the file

To recreate the example above, skipping .svn directories, one would run:

$ python -m compileall -x '/\.svn' examples
Listing examples ...
Compiling examples/a.py ...
Listing examples/subdir ...
Compiling examples/subdir/b.py ...

See also

compileall
The standard library documentation for this module.

PyMOTW Home


You might also be interested in:

News Topics

Recommended for You

Got a Question?