- py_compile —- Compile Python source files
py_compile —- Compile Python source files
Source code:Lib/py_compile.py
The py_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
py_compile.
PyCompileError
Exception raised when an error occurs while attempting to compile the file.
pycompile.
compile
(_file, cfile=None, dfile=None, doraise=False, optimize=-1, invalidation_mode=PycInvalidationMode.TIMESTAMP)- Compile a source file to byte-code and write out the byte-code cache file.The source code is loaded from the file named file. The byte-code iswritten to cfile, which defaults to the PEP 3147/PEP 488 path, endingin
.pyc
.For example, if file is/foo/bar/baz.py
cfile will default to/foo/bar/pycache/baz.cpython-32.pyc
for Python 3.2. If dfile isspecified, it is used as the name of the source file in error messages wheninstead of file. If doraise is true, aPyCompileError
is raisedwhen an error is encountered while compiling file. If doraise 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.whatever cfile value was used.
The doraise and quiet arguments determine how errors are handled whilecompiling file. If quiet is 0 or 1, and doraise is false, the defaultbehaviour is enabled: an error string is written to sys.stderr
, and thefunction returns None
instead of a path. If doraise is true,a PyCompileError
is raised instead. However if quiet is 2,no message is written, and doraise has no effect.
If the path that cfile 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.
invalidation_mode should be a member of the PycInvalidationMode
enum and controls how the generated bytecode cache is invalidated atruntime. The default is PycInvalidationMode.CHECKED_HASH
ifthe SOURCE_DATE_EPOCH
environment variable is set, otherwisethe default is PycInvalidationMode.TIMESTAMP
.
在 3.2 版更改: Changed default value of cfile to be PEP 3147-compliant. Previousdefault was file + 'c'
('o'
if optimization was enabled).Also added the optimize parameter.
在 3.4 版更改: Changed code to use importlib
for the byte-code cache file writing.This means file creation/writing semantics now match what importlib
does, e.g. permissions, write-and-move semantics, etc. Also added thecaveat that FileExistsError
is raised if cfile is a symlink ornon-regular file.
在 3.7 版更改: The invalidation_mode parameter was added as specified in PEP 552.If the SOURCEDATE_EPOCH
environment variable is set,_invalidation_mode will be forced toPycInvalidationMode.CHECKED_HASH
.
在 3.7.2 版更改: The SOURCEDATE_EPOCH
environment variable no longeroverrides the value of the _invalidation_mode argument, and determinesits default value instead.
在 3.8 版更改: The quiet parameter was added.
- class
py_compile.
PycInvalidationMode
- A enumeration of possible methods the interpreter can use to determinewhether a bytecode file is up to date with a source file. The
.pyc
fileindicates the desired invalidation mode in its header. See已缓存字节码的失效 for more information on how Python invalidates.pyc
files at runtime.
3.7 新版功能.
TIMESTAMP
The
.pyc
file includes the timestamp and size of the source file,which Python will compare against the metadata of the source file atruntime to determine if the.pyc
file needs to be regenerated.CHECKED_HASH
The
.pyc
file includes a hash of the source file content, which Pythonwill compare against the source at runtime to determine if the.pyc
file needs to be regenerated.UNCHECKED_HASH
- Like
CHECKED_HASH
, the.pyc
file includes a hash of the sourcefile content. However, Python will at runtime assume the.pyc
file isup to date and not validate the.pyc
against the source file at all.
This option is useful when the .pycs
are kept up to date by somesystem external to Python like a build system.
pycompile.
main
(_args=None)- Compile several source files. The files named in args (or on the commandline, if args is
None
) 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.
在 3.2 版更改: Added support for '-'
.
When this module is run as a script, the main()
is used to compile all thefiles named on the command line. The exit status is nonzero if one of the filescould not be compiled.
参见
- Module
compileall
- Utilities to compile all Python source files in a directory tree.