17db96d56Sopenharmony_ci:mod:`py_compile` --- Compile Python source files 27db96d56Sopenharmony_ci================================================= 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: py_compile 57db96d56Sopenharmony_ci :synopsis: Generate byte-code files from Python source files. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 87db96d56Sopenharmony_ci.. documentation based on module docstrings 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ci**Source code:** :source:`Lib/py_compile.py` 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci.. index:: pair: file; byte-code 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci-------------- 157db96d56Sopenharmony_ci 167db96d56Sopenharmony_ciThe :mod:`py_compile` module provides a function to generate a byte-code file 177db96d56Sopenharmony_cifrom a source file, and another function used when the module source file is 187db96d56Sopenharmony_ciinvoked as a script. 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ciThough not often needed, this function can be useful when installing modules for 217db96d56Sopenharmony_cishared use, especially if some of the users may not have permission to write the 227db96d56Sopenharmony_cibyte-code cache files in the directory containing the source code. 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci.. exception:: PyCompileError 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ci Exception raised when an error occurs while attempting to compile the file. 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ci 307db96d56Sopenharmony_ci.. function:: compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, invalidation_mode=PycInvalidationMode.TIMESTAMP, quiet=0) 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ci Compile a source file to byte-code and write out the byte-code cache file. 337db96d56Sopenharmony_ci The source code is loaded from the file named *file*. The byte-code is 347db96d56Sopenharmony_ci written to *cfile*, which defaults to the :pep:`3147`/:pep:`488` path, ending 357db96d56Sopenharmony_ci in ``.pyc``. 367db96d56Sopenharmony_ci For example, if *file* is ``/foo/bar/baz.py`` *cfile* will default to 377db96d56Sopenharmony_ci ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2. If *dfile* is 387db96d56Sopenharmony_ci specified, it is used instead of *file* as the name of the source file from 397db96d56Sopenharmony_ci which source lines are obtained for display in exception tracebacks. 407db96d56Sopenharmony_ci If *doraise* is true, a :exc:`PyCompileError` is raised 417db96d56Sopenharmony_ci when an error is encountered while compiling *file*. If *doraise* is false 427db96d56Sopenharmony_ci (the default), an error string is written to ``sys.stderr``, but no exception 437db96d56Sopenharmony_ci is raised. This function returns the path to byte-compiled file, i.e. 447db96d56Sopenharmony_ci whatever *cfile* value was used. 457db96d56Sopenharmony_ci 467db96d56Sopenharmony_ci The *doraise* and *quiet* arguments determine how errors are handled while 477db96d56Sopenharmony_ci compiling file. If *quiet* is 0 or 1, and *doraise* is false, the default 487db96d56Sopenharmony_ci behaviour is enabled: an error string is written to ``sys.stderr``, and the 497db96d56Sopenharmony_ci function returns ``None`` instead of a path. If *doraise* is true, 507db96d56Sopenharmony_ci a :exc:`PyCompileError` is raised instead. However if *quiet* is 2, 517db96d56Sopenharmony_ci no message is written, and *doraise* has no effect. 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ci If the path that *cfile* becomes (either explicitly specified or computed) 547db96d56Sopenharmony_ci is a symlink or non-regular file, :exc:`FileExistsError` will be raised. 557db96d56Sopenharmony_ci This is to act as a warning that import will turn those paths into regular 567db96d56Sopenharmony_ci files if it is allowed to write byte-compiled files to those paths. This is 577db96d56Sopenharmony_ci a side-effect of import using file renaming to place the final byte-compiled 587db96d56Sopenharmony_ci file into place to prevent concurrent file writing issues. 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ci *optimize* controls the optimization level and is passed to the built-in 617db96d56Sopenharmony_ci :func:`compile` function. The default of ``-1`` selects the optimization 627db96d56Sopenharmony_ci level of the current interpreter. 637db96d56Sopenharmony_ci 647db96d56Sopenharmony_ci *invalidation_mode* should be a member of the :class:`PycInvalidationMode` 657db96d56Sopenharmony_ci enum and controls how the generated bytecode cache is invalidated at 667db96d56Sopenharmony_ci runtime. The default is :attr:`PycInvalidationMode.CHECKED_HASH` if 677db96d56Sopenharmony_ci the :envvar:`SOURCE_DATE_EPOCH` environment variable is set, otherwise 687db96d56Sopenharmony_ci the default is :attr:`PycInvalidationMode.TIMESTAMP`. 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ci .. versionchanged:: 3.2 717db96d56Sopenharmony_ci Changed default value of *cfile* to be :PEP:`3147`-compliant. Previous 727db96d56Sopenharmony_ci default was *file* + ``'c'`` (``'o'`` if optimization was enabled). 737db96d56Sopenharmony_ci Also added the *optimize* parameter. 747db96d56Sopenharmony_ci 757db96d56Sopenharmony_ci .. versionchanged:: 3.4 767db96d56Sopenharmony_ci Changed code to use :mod:`importlib` for the byte-code cache file writing. 777db96d56Sopenharmony_ci This means file creation/writing semantics now match what :mod:`importlib` 787db96d56Sopenharmony_ci does, e.g. permissions, write-and-move semantics, etc. Also added the 797db96d56Sopenharmony_ci caveat that :exc:`FileExistsError` is raised if *cfile* is a symlink or 807db96d56Sopenharmony_ci non-regular file. 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ci .. versionchanged:: 3.7 837db96d56Sopenharmony_ci The *invalidation_mode* parameter was added as specified in :pep:`552`. 847db96d56Sopenharmony_ci If the :envvar:`SOURCE_DATE_EPOCH` environment variable is set, 857db96d56Sopenharmony_ci *invalidation_mode* will be forced to 867db96d56Sopenharmony_ci :attr:`PycInvalidationMode.CHECKED_HASH`. 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ci .. versionchanged:: 3.7.2 897db96d56Sopenharmony_ci The :envvar:`SOURCE_DATE_EPOCH` environment variable no longer 907db96d56Sopenharmony_ci overrides the value of the *invalidation_mode* argument, and determines 917db96d56Sopenharmony_ci its default value instead. 927db96d56Sopenharmony_ci 937db96d56Sopenharmony_ci .. versionchanged:: 3.8 947db96d56Sopenharmony_ci The *quiet* parameter was added. 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci 977db96d56Sopenharmony_ci.. class:: PycInvalidationMode 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ci A enumeration of possible methods the interpreter can use to determine 1007db96d56Sopenharmony_ci whether a bytecode file is up to date with a source file. The ``.pyc`` file 1017db96d56Sopenharmony_ci indicates the desired invalidation mode in its header. See 1027db96d56Sopenharmony_ci :ref:`pyc-invalidation` for more information on how Python invalidates 1037db96d56Sopenharmony_ci ``.pyc`` files at runtime. 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ci .. versionadded:: 3.7 1067db96d56Sopenharmony_ci 1077db96d56Sopenharmony_ci .. attribute:: TIMESTAMP 1087db96d56Sopenharmony_ci 1097db96d56Sopenharmony_ci The ``.pyc`` file includes the timestamp and size of the source file, 1107db96d56Sopenharmony_ci which Python will compare against the metadata of the source file at 1117db96d56Sopenharmony_ci runtime to determine if the ``.pyc`` file needs to be regenerated. 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci .. attribute:: CHECKED_HASH 1147db96d56Sopenharmony_ci 1157db96d56Sopenharmony_ci The ``.pyc`` file includes a hash of the source file content, which Python 1167db96d56Sopenharmony_ci will compare against the source at runtime to determine if the ``.pyc`` 1177db96d56Sopenharmony_ci file needs to be regenerated. 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ci .. attribute:: UNCHECKED_HASH 1207db96d56Sopenharmony_ci 1217db96d56Sopenharmony_ci Like :attr:`CHECKED_HASH`, the ``.pyc`` file includes a hash of the source 1227db96d56Sopenharmony_ci file content. However, Python will at runtime assume the ``.pyc`` file is 1237db96d56Sopenharmony_ci up to date and not validate the ``.pyc`` against the source file at all. 1247db96d56Sopenharmony_ci 1257db96d56Sopenharmony_ci This option is useful when the ``.pycs`` are kept up to date by some 1267db96d56Sopenharmony_ci system external to Python like a build system. 1277db96d56Sopenharmony_ci 1287db96d56Sopenharmony_ci 1297db96d56Sopenharmony_ciCommand-Line Interface 1307db96d56Sopenharmony_ci---------------------- 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ciThis module can be invoked as a script to compile several source 1337db96d56Sopenharmony_cifiles. The files named in *filenames* are compiled and the resulting 1347db96d56Sopenharmony_cibytecode is cached in the normal manner. This program does not search 1357db96d56Sopenharmony_cia directory structure to locate source files; it only compiles files 1367db96d56Sopenharmony_cinamed explicitly. The exit status is nonzero if one of the files could 1377db96d56Sopenharmony_cinot be compiled. 1387db96d56Sopenharmony_ci 1397db96d56Sopenharmony_ci.. program:: python -m py_compile 1407db96d56Sopenharmony_ci 1417db96d56Sopenharmony_ci.. cmdoption:: <file> ... <fileN> 1427db96d56Sopenharmony_ci - 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci Positional arguments are files to compile. If ``-`` is the only 1457db96d56Sopenharmony_ci parameter, the list of files is taken from standard input. 1467db96d56Sopenharmony_ci 1477db96d56Sopenharmony_ci.. cmdoption:: -q, --quiet 1487db96d56Sopenharmony_ci 1497db96d56Sopenharmony_ci Suppress errors output. 1507db96d56Sopenharmony_ci 1517db96d56Sopenharmony_ci.. versionchanged:: 3.2 1527db96d56Sopenharmony_ci Added support for ``-``. 1537db96d56Sopenharmony_ci 1547db96d56Sopenharmony_ci.. versionchanged:: 3.10 1557db96d56Sopenharmony_ci Added support for :option:`-q`. 1567db96d56Sopenharmony_ci 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ci.. seealso:: 1597db96d56Sopenharmony_ci 1607db96d56Sopenharmony_ci Module :mod:`compileall` 1617db96d56Sopenharmony_ci Utilities to compile all Python source files in a directory tree. 162