Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python How to move/centralize pycache files

Tags:

python

Below is what my project looks like

Project
  src
    folder1
      __pycache__
      ...
    folder2
      __pycache__
      ...
    main
    __pycache__

  tests
    __pycache__
      ...

Is there a way to centralize all the pycache files in one folder so that it looks something like this?

Project
  __pycache__
  src
    folder1
    folder2
    main
  tests


  
like image 300
Michael Xia Avatar asked Aug 04 '26 19:08

Michael Xia


1 Answers

It is not supported to put all cache files under a single subdirectory.

There is an option to put the cache directory tree someplace else, however. Set PYTHONPYCACHEPREFIX:

export PYTHONPYCACHEPREFIX=/path/to/someplace/else

This will create the cached directory tree under /path/to/someplace/else and not litter your project directory with __pycache__ subdirectories.

If you want to disable caching entirely, use PYTHONDONTWRITEBYTECODE instead. Note that startup times will be slightly slower, as Python would have to recompute .pyc files on every startup.

These options may also be specified via interpreter command-line options, -X or -B respectively, rather than via environment variable.

like image 180
wim Avatar answered Aug 07 '26 09:08

wim