Python | Compileall Module

Python | Compileall Module

----- compileall MODULE -----

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

This module provides some utility functions to support installing Python libraries. These
functions compile Python source files in a directory tree. This module can be used to create the cached byte-code files at library installation time, which makes them available for use even by users who don't have write permission to the library directories.

----- Compiling One Directory -----

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

Code -

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:

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

Code -

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

Code -

----- Compilingsys.path -----

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

Code -

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.

----- 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:

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

Other Links : Compileall

Thank You