3

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


  
Michael Xia
  • 190
  • 8
  • Related: Sometimes you might just want to hide the `__pycache__` in your IDE. [Here's](https://stackoverflow.com/questions/30140112/how-do-i-hide-certain-files-from-the-sidebar-in-visual-studio-code) how to do that in VS Code, and [here](https://stackoverflow.com/questions/23516219/pycharm-hide-files-by-pattern) how to do that in PyCharm. – Niko Föhr Jul 16 '23 at 20:19

2 Answers2

2

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 leave your project directory unlittered 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.

wim
  • 338,267
  • 99
  • 616
  • 750
0

See this

You can set the PYTHONPYCACHEPREFIX environment variable to the desired path.

LITzman
  • 730
  • 1
  • 16