Is it safe to delete __pycache__ folders?
- Yes. Every file in
__pycache__is compiled from a.pyfile beside it, and Python compiles it again by itself. Nothing in there is the only copy of your code. - What it costs you: the next import of each module is a little slower while Python compiles it again. Code doesn’t run slower once it’s loaded.
- Does it come back? Yes, as soon as the code runs again, unless you tell Python not to write these files.
- SiftMac’s verdict: Rebuilds
1. What’s in __pycache__
When Python imports a module, it compiles the source to bytecode and saves the result in a __pycache__ folder next to the .py file. The file name carries the module and the Python version, like spam.cpython-33.pyc in Python’s own example, so several Python versions can keep their files side by side.
Before using a cached file, Python compares it with the source’s modification date and recompiles when the source has changed. Python’s tutorial calls this a completely automatic process. It also notes that a program doesn’t run any faster from a .pyc than from a .py; only loading is faster.
- The script you run directly is never cached, only the modules it imports.
- A
.pycin__pycache__is never used without its source. Shipping compiled-only code requires the.pycin the source folder itself, so clearing__pycache__can’t take away the only copy of anything.
2. Where they appear
Next to imported .py files wherever Python can write: in your projects, inside each virtual environment’s site-packages, and in tools you’ve installed. To count and measure the ones in one project:
cd ~/Projects/my-app
find . -type d -name __pycache__ -prune | wc -l
find . -type d -name __pycache__ -prune -exec du -sk {} + | awk '{s+=$1} END {print s/1024 " MB"}'
3. How much space they take
They’re compiled copies of source files, so their total follows the amount of Python involved: modest in a project of your own, larger inside a virtual environment full of installed packages. The commands above give your number.
If space is what you’re after, an old project’s virtual environment is the bigger target, and it takes its own __pycache__ folders with it.
4. How to clear them
From the top of a project, this removes every __pycache__ folder under it and nothing else:
cd ~/Projects/my-app
find . -type d -name __pycache__ -prune -exec rm -rf {} +
Run it inside the project folder, never from your home folder or /, and run the find part on its own first if you want to see the list. Python programs already running carry on; their modules are loaded.
Stopping them from appearing
python -B, orPYTHONDONTWRITEBYTECODE=1in your environment, tells Python not to write.pycfiles on import. Imports then compile every time.PYTHONPYCACHEPREFIX, in Python 3.8 and later, keeps the files but writes them to a mirror tree in one folder instead of__pycache__folders in your code.
export PYTHONPYCACHEPREFIX="$HOME/Library/Caches/pycache"
5. What happens afterwards
- The first import of each module compiles it again and writes a fresh
.pyc, so the first run of a test suite or a big app starts a little slower. - Everything else stays the same: same code, same results.
- Folders appear again in the same places the next time the code runs, unless you turned writing off or moved it with
PYTHONPYCACHEPREFIX.
Questions
Will deleting __pycache__ break my Python project?
No. Python checks the cache against the source on each import and rebuilds anything missing or out of date. The worst case is a slightly slower first start.
Why are there several .pyc files for one module?
Each Python version writes its own, with the version in the file name, so two Python versions can use the same project without overwriting each other’s files.
Should I delete __pycache__ inside site-packages?
You can; they come back the next time those packages are imported. If the environment belongs to a project you’ve finished, deleting the whole environment is the better move.
Does deleting .pyc files make Python slower?
Only at import time, and only once. Python’s tutorial points out that code doesn’t run any faster from a .pyc; the cache only speeds up loading.
How SiftMac handles it. SiftMac has no category for __pycache__ folders. Its space map recognises them as compiled Python files that Python makes again, and its duplicate finder skips them, so identical .pyc files are never offered as duplicates. If you point PYTHONPYCACHEPREFIX into ~/Library/Caches, that folder shows up with the other app caches.
Download SiftMac · scanning is free, and so is your first 2 GB of cleaning · See how it works