@@ -183,57 +183,45 @@ directory. This is an error unless the replacement is intended. See section
183183 "Compiled" Python files
184184-----------------------
185185
186- As an important speed-up of the start-up time for short programs that use a lot
187- of standard modules, if a file called :file: `spam.pyc ` exists in the directory
188- where :file: `spam.py ` is found, this is assumed to contain an
189- already-"byte-compiled" version of the module :mod: `spam `. The modification time
190- of the version of :file: `spam.py ` used to create :file: `spam.pyc ` is recorded in
191- :file: `spam.pyc `, and the :file: `.pyc ` file is ignored if these don't match.
192-
193- Normally, you don't need to do anything to create the :file: `spam.pyc ` file.
194- Whenever :file: `spam.py ` is successfully compiled, an attempt is made to write
195- the compiled version to :file: `spam.pyc `. It is not an error if this attempt
196- fails; if for any reason the file is not written completely, the resulting
197- :file: `spam.pyc ` file will be recognized as invalid and thus ignored later. The
198- contents of the :file: `spam.pyc ` file are platform independent, so a Python
199- module directory can be shared by machines of different architectures.
186+ To speed up loading modules, Python caches the compiled version of each module
187+ in the ``__pycache__ `` directory under the name :file: `module.{ version } .pyc` `,
188+ where the version encodes the format of the compiled file; it generally contains
189+ the Python version number. For example, in CPython release 3.3 the compiled
190+ version of spam.py would be cached as ``__pycache__/spam.cpython-33.pyc ``. This
191+ naming convention allows compiled modules from different releases and different
192+ versions of Python to coexist.
193+
194+ Python checks the modification date of the source against the compiled version
195+ to see if it's out of date and needs to be recompiled. This is a completely
196+ automatic process. Also, the compiled modules are platform-independent, so the
197+ same library can be shared among systems with different architectures.
198+
199+ Python does not check the cache in two circumstances. First, it always
200+ recompiles and does not store the result for the module that's loaded directly
201+ from the command line. Second, it does not check the cache if there is no
202+ source module. To support a non-source (compiled only) distribution, the
203+ compiled module must be in the source directory, and there must not be a source
204+ module.
200205
201206Some tips for experts:
202207
203- * When the Python interpreter is invoked with the :option: `-O ` flag, optimized
204- code is generated and stored in :file: `.pyo ` files. The optimizer currently
205- doesn't help much; it only removes :keyword: `assert ` statements. When
206- :option: `-O ` is used, *all * :term: `bytecode ` is optimized; ``.pyc `` files are
207- ignored and ``.py `` files are compiled to optimized bytecode.
208-
209- * Passing two :option: `-O ` flags to the Python interpreter (:option: `-OO `) will
210- cause the bytecode compiler to perform optimizations that could in some rare
211- cases result in malfunctioning programs. Currently only ``__doc__ `` strings are
212- removed from the bytecode, resulting in more compact :file: `.pyo ` files. Since
213- some programs may rely on having these available, you should only use this
214- option if you know what you're doing.
215-
216- * A program doesn't run any faster when it is read from a :file: `.pyc ` or
217- :file: `.pyo ` file than when it is read from a :file: `.py ` file; the only thing
218- that's faster about :file: `.pyc ` or :file: `.pyo ` files is the speed with which
219- they are loaded.
220-
221- * When a script is run by giving its name on the command line, the bytecode for
222- the script is never written to a :file: `.pyc ` or :file: `.pyo ` file. Thus, the
223- startup time of a script may be reduced by moving most of its code to a module
224- and having a small bootstrap script that imports that module. It is also
225- possible to name a :file: `.pyc ` or :file: `.pyo ` file directly on the command
226- line.
227-
228- * It is possible to have a file called :file: `spam.pyc ` (or :file: `spam.pyo `
229- when :option: `-O ` is used) without a file :file: `spam.py ` for the same module.
230- This can be used to distribute a library of Python code in a form that is
231- moderately hard to reverse engineer.
232-
233- .. index :: module: compileall
234-
235- * The module :mod: `compileall ` can create :file: `.pyc ` files (or :file: `.pyo `
236- files when :option: `-O ` is used) for all modules in a directory.
208+ * You can use the :option: `-O ` or :option: `-OO ` switches on the Python command
209+ to reduce the size of a compiled module. The ``-O `` switch removes assert
210+ statements, the ``-OO `` switch removes both assert statements and __doc__
211+ strings. Since some programs may rely on having these available, you should
212+ only use this option if you know what you're doing. "Optimized" modules have
213+ a .pyo rather than a .pyc suffix and are usually smaller. Future releases may
214+ change the effects of optimization.
215+
216+ * A program doesn't run any faster when it is read from a ``.pyc `` or ``.pyo ``
217+ file than when it is read from a ``.py `` file; the only thing that's faster
218+ about ``.pyc `` or ``.pyo `` files is the speed with which they are loaded.
219+
220+ * The module :mod: `compileall ` can create .pyc files (or .pyo files when
221+ :option: `-O ` is used) for all modules in a directory.
222+
223+ * There is more detail on this process, including a flow chart of the
224+ decisions, in PEP 3147.
237225
238226
239227.. _tut-standardmodules :
0 commit comments