pythonpython-packaging

Is it possible to make __pycache__ generate in a single centralized folder?


I have a project configured with pyproject.toml and setuptools.build_meta as build backend, developed using editable install in a venv. So far I've managed to redirect other temporary folders to a single centralized folder build in the root of the repository, stuff like .egg-info and pytest_cache, as they expose config settings that can be set in the pyproject.toml.

Is it possible to do something like that for __pycache__ folders, so they won't be littered all around the code folders?


Solution

  • Yes, since Python 3.8:

    PYTHONPYCACHEPREFIX

    If this [environment variable] 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 -X pycache_prefix=PATH option.