compileall – Byte-compile Source Files

Purpose:Convert source files to byte-compiled version.
Available In: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/text-base ...
Compiling examples/a.py ...
Listing examples/subdir ...
Listing examples/subdir/.svn ...
Listing examples/subdir/.svn/prop-base ...
Listing examples/subdir/.svn/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 ...
Listing examples/.svn ...
Listing examples/.svn/prop-base ...
Listing examples/.svn/text-base ...
Compiling examples/a.py ...
Listing examples/subdir ...
Listing examples/subdir/.svn ...
Listing examples/subdir/.svn/prop-base ...
Listing examples/subdir/.svn/text-base ...
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] [-i list] [directory|file ...]
-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 searched for in the full path of the file
-i list: expand list with its content (file and directory names)

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

$ python -m compileall -x '/\.svn' examples

Listing examples ...
Listing examples/.svn ...
Listing examples/.svn/prop-base ...
Listing examples/.svn/text-base ...
Compiling examples/a.py ...
Listing examples/subdir ...
Listing examples/subdir/.svn ...
Listing examples/subdir/.svn/prop-base ...
Listing examples/subdir/.svn/text-base ...
Compiling examples/subdir/b.py ...

See also

compileall
The standard library documentation for this module.