32.10.py_compile — Compile Python source files

Source code:Lib/py_compile.py


Thepy_compile module provides a function to generate a byte-code filefrom a source file, and another function used when the module source file isinvoked as a script.

Though not often needed, this function can be useful when installing modules forshared use, especially if some of the users may not have permission to write thebyte-code cache files in the directory containing the source code.

exceptionpy_compile.PyCompileError

Exception raised when an error occurs while attempting to compile the file.

py_compile.compile(file,cfile=None,dfile=None,doraise=False,optimize=-1)

Compile a source file to byte-code and write out the byte-code cache file.The source code is loaded from the file namedfile. The byte-code iswritten tocfile, which defaults to thePEP 3147/PEP 488 path, endingin.pyc.For example, iffile is/foo/bar/baz.pycfile will default to/foo/bar/__pycache__/baz.cpython-32.pyc for Python 3.2. Ifdfile isspecified, it is used as the name of the source file in error messages wheninstead offile. Ifdoraise is true, aPyCompileError is raisedwhen an error is encountered while compilingfile. Ifdoraise is false(the default), an error string is written tosys.stderr, but no exceptionis raised. This function returns the path to byte-compiled file, i.e.whatevercfile value was used.

If the path thatcfile becomes (either explicitly specified or computed)is a symlink or non-regular file,FileExistsError will be raised.This is to act as a warning that import will turn those paths into regularfiles if it is allowed to write byte-compiled files to those paths. This isa side-effect of import using file renaming to place the final byte-compiledfile into place to prevent concurrent file writing issues.

optimize controls the optimization level and is passed to the built-incompile() function. The default of-1 selects the optimizationlevel of the current interpreter.

Changed in version 3.2:Changed default value ofcfile to bePEP 3147-compliant. Previousdefault wasfile +'c' ('o' if optimization was enabled).Also added theoptimize parameter.

Changed in version 3.4:Changed code to useimportlib for the byte-code cache file writing.This means file creation/writing semantics now match whatimportlibdoes, e.g. permissions, write-and-move semantics, etc. Also added thecaveat thatFileExistsError is raised ifcfile is a symlink ornon-regular file.

py_compile.main(args=None)

Compile several source files. The files named inargs (or on the commandline, ifargs isNone) are compiled and the resulting byte-code iscached in the normal manner. This function does not search a directorystructure to locate source files; it only compiles files named explicitly.If'-' is the only parameter in args, the list of files is taken fromstandard input.

Changed in version 3.2:Added support for'-'.

When this module is run as a script, themain() is used to compile all thefiles named on the command line. The exit status is nonzero if one of the filescould not be compiled.

See also

Modulecompileall
Utilities to compile all Python source files in a directory tree.