bpo-33499: Add PYTHONPYCACHEPREFIX env var for alt bytecode cache location. (GH-6834)

In some development setups it is inconvenient or impossible to write bytecode
caches to the code tree, but the bytecode caches are still useful. The
PYTHONPYCACHEPREFIX environment variable allows specifying an alternate
location for cached bytecode files, within which a directory tree mirroring the code
tree will be created. This cache tree is then used (for both reading and writing)
instead of the local `__pycache__` subdirectory within each source directory.

Exposed at runtime as sys.pycache_prefix (defaulting to None), and can
be set from the CLI as "-X pycache_prefix=path".

Patch by Carl Meyer.
This commit is contained in:
Carl Meyer 2018-06-15 22:40:56 -06:00 committed by Nick Coghlan
parent 6868003514
commit b193fa996a
12 changed files with 1386 additions and 1087 deletions

View file

@ -442,6 +442,9 @@ Miscellaneous options
the default locale-aware mode. ``-X utf8=0`` explicitly disables UTF-8
mode (even when it would otherwise activate automatically).
See :envvar:`PYTHONUTF8` for more details.
* ``-X pycache_prefix=PATH`` enables writing ``.pyc`` files to a parallel
tree rooted at the given directory instead of to the code tree. See also
:envvar:`PYTHONPYCACHEPREFIX`.
It also allows passing arbitrary values and retrieving them through the
:data:`sys._xoptions` dictionary.
@ -461,6 +464,9 @@ Miscellaneous options
.. versionadded:: 3.7
The ``-X importtime``, ``-X dev`` and ``-X utf8`` options.
.. versionadded:: 3.8
The ``-X pycache_prefix`` option.
Options you shouldn't use
~~~~~~~~~~~~~~~~~~~~~~~~~
@ -587,6 +593,16 @@ conflict.
specifying the :option:`-B` option.
.. envvar:: PYTHONPYCACHEPREFIX
If this is set, Python will write ``.pyc`` files in a mirror directory tree
at this path, instead of in ``__pycache__`` directories within the source
tree. This is equivalent to specifying the :option:`-X`
``pycache_prefix=PATH`` option.
.. versionadded:: 3.8
.. envvar:: PYTHONHASHSEED
If this variable is not set or set to ``random``, a random value is used