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.
Exception raised when an error occurs while attempting to compile the file.
Compile a source file to byte-code and write out the byte-code cache file.The source code is loaded from the file namefile. The byte-code iswritten tocfile, which defaults to thePEP 3147 path, ending in.pyc (.pyo if optimization is enabled in the current interpreter).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.
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.
Compile several source files. The files named inargs (or on the commandline, ifargs isNone) are compiled and the resulting bytecode 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
31.9.pyclbr — Python class browser support
31.11.compileall — Byte-compile Python libraries
Enter search terms or a module, class or function name.