Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`py_compile` --- Compile Python source files |
| 2 | ================================================= |
| 3 | |
| 4 | .. module:: py_compile |
| 5 | :synopsis: Generate byte-code files from Python source files. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | .. documentation based on module docstrings |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/py_compile.py` |
| 11 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 12 | .. index:: pair: file; byte-code |
| 13 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 14 | -------------- |
| 15 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | The :mod:`py_compile` module provides a function to generate a byte-code file |
| 17 | from a source file, and another function used when the module source file is |
| 18 | invoked as a script. |
| 19 | |
| 20 | Though not often needed, this function can be useful when installing modules for |
| 21 | shared use, especially if some of the users may not have permission to write the |
| 22 | byte-code cache files in the directory containing the source code. |
| 23 | |
| 24 | |
| 25 | .. exception:: PyCompileError |
| 26 | |
| 27 | Exception raised when an error occurs while attempting to compile the file. |
| 28 | |
| 29 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 30 | .. function:: compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, invalidation_mode=PycInvalidationMode.TIMESTAMP) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
Brett Cannon | aa73a1c | 2013-03-13 09:37:42 -0700 | [diff] [blame] | 32 | Compile a source file to byte-code and write out the byte-code cache file. |
R David Murray | 689016f | 2016-08-13 14:47:18 -0400 | [diff] [blame] | 33 | The source code is loaded from the file named *file*. The byte-code is |
Larry Hastings | 770ce20 | 2015-04-19 13:50:12 -0700 | [diff] [blame] | 34 | written to *cfile*, which defaults to the :pep:`3147`/:pep:`488` path, ending |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 35 | in ``.pyc``. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 36 | For example, if *file* is ``/foo/bar/baz.py`` *cfile* will default to |
| 37 | ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2. If *dfile* is |
| 38 | specified, it is used as the name of the source file in error messages when |
| 39 | instead of *file*. If *doraise* is true, a :exc:`PyCompileError` is raised |
| 40 | when an error is encountered while compiling *file*. If *doraise* is false |
| 41 | (the default), an error string is written to ``sys.stderr``, but no exception |
| 42 | is raised. This function returns the path to byte-compiled file, i.e. |
| 43 | whatever *cfile* value was used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 44 | |
Brett Cannon | 33915eb | 2013-06-14 18:33:00 -0400 | [diff] [blame] | 45 | If the path that *cfile* becomes (either explicitly specified or computed) |
| 46 | is a symlink or non-regular file, :exc:`FileExistsError` will be raised. |
| 47 | This is to act as a warning that import will turn those paths into regular |
| 48 | files if it is allowed to write byte-compiled files to those paths. This is |
| 49 | a side-effect of import using file renaming to place the final byte-compiled |
| 50 | file into place to prevent concurrent file writing issues. |
| 51 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 52 | *optimize* controls the optimization level and is passed to the built-in |
| 53 | :func:`compile` function. The default of ``-1`` selects the optimization |
| 54 | level of the current interpreter. |
| 55 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 56 | *invalidation_mode* should be a member of the :class:`PycInvalidationMode` |
| 57 | enum and controls how the generated ``.pyc`` files are invalidated at |
| 58 | runtime. |
| 59 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 60 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 61 | Changed default value of *cfile* to be :PEP:`3147`-compliant. Previous |
| 62 | default was *file* + ``'c'`` (``'o'`` if optimization was enabled). |
| 63 | Also added the *optimize* parameter. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 64 | |
Brett Cannon | aa73a1c | 2013-03-13 09:37:42 -0700 | [diff] [blame] | 65 | .. versionchanged:: 3.4 |
| 66 | Changed code to use :mod:`importlib` for the byte-code cache file writing. |
| 67 | This means file creation/writing semantics now match what :mod:`importlib` |
Brett Cannon | 33915eb | 2013-06-14 18:33:00 -0400 | [diff] [blame] | 68 | does, e.g. permissions, write-and-move semantics, etc. Also added the |
| 69 | caveat that :exc:`FileExistsError` is raised if *cfile* is a symlink or |
| 70 | non-regular file. |
Brett Cannon | aa73a1c | 2013-03-13 09:37:42 -0700 | [diff] [blame] | 71 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 72 | .. versionchanged:: 3.7 |
| 73 | The *invalidation_mode* parameter was added as specified in :pep:`552`. |
| 74 | |
| 75 | |
| 76 | .. class:: PycInvalidationMode |
| 77 | |
| 78 | A enumeration of possible methods the interpreter can use to determine |
| 79 | whether a bytecode file is up to date with a source file. The ``.pyc`` file |
| 80 | indicates the desired invalidation mode in its header. See |
| 81 | :ref:`pyc-invalidation` for more information on how Python invalidates |
| 82 | ``.pyc`` files at runtime. |
| 83 | |
| 84 | .. versionadded:: 3.7 |
| 85 | |
| 86 | .. attribute:: TIMESTAMP |
| 87 | |
| 88 | The ``.pyc`` file includes the timestamp and size of the source file, |
| 89 | which Python will compare against the metadata of the source file at |
| 90 | runtime to determine if the ``.pyc`` file needs to be regenerated. |
| 91 | |
| 92 | .. attribute:: CHECKED_HASH |
| 93 | |
| 94 | The ``.pyc`` file includes a hash of the source file content, which Python |
| 95 | will compare against the source at runtime to determine if the ``.pyc`` |
| 96 | file needs to be regenerated. |
| 97 | |
| 98 | .. attribute:: UNCHECKED_HASH |
| 99 | |
| 100 | Like :attr:`CHECKED_HASH`, the ``.pyc`` file includes a hash of the source |
| 101 | file content. However, Python will at runtime assume the ``.pyc`` file is |
| 102 | up to date and not validate the ``.pyc`` against the source file at all. |
| 103 | |
| 104 | This option is useful when the ``.pycs`` are kept up to date by some |
| 105 | system external to Python like a build system. |
| 106 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 108 | .. function:: main(args=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
| 110 | Compile several source files. The files named in *args* (or on the command |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 111 | line, if *args* is ``None``) are compiled and the resulting byte-code is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | cached in the normal manner. This function does not search a directory |
| 113 | structure to locate source files; it only compiles files named explicitly. |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 114 | If ``'-'`` is the only parameter in args, the list of files is taken from |
| 115 | standard input. |
| 116 | |
| 117 | .. versionchanged:: 3.2 |
| 118 | Added support for ``'-'``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
| 120 | When this module is run as a script, the :func:`main` is used to compile all the |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 121 | files named on the command line. The exit status is nonzero if one of the files |
| 122 | could not be compiled. |
| 123 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | |
| 125 | .. seealso:: |
| 126 | |
| 127 | Module :mod:`compileall` |
| 128 | Utilities to compile all Python source files in a directory tree. |