Skip to content

Every Python project keeps its own copy of everything

A virtualenv per project means the same libraries stored dozens of times. Add the pip cache and bytecode and it is routinely tens of gigabytes.

6 min read

Python's isolation model is one environment per project, which is correct and is also why the disk fills. Ten projects using the same data libraries store ten copies of them, and nothing ever notices that they are identical.

Three separate piles

WhatWhereRegenerates?
Virtual environments.venv or venv in each projectYes, pip install
Package cache~/Library/Caches/pipYes, on next download
Bytecode__pycache__ folders everywhereYes, on next run

All three are safe to remove. The only cost is that the next install or run is slower once.

Measuring it honestly

The environments are the large one, and they are scattered, so a single du on one project tells you nothing:

# Every virtualenv under your home folder, largest first
find ~ -type d \( -name ".venv" -o -name "venv" \) -prune -print0 2>/dev/null \
  | xargs -0 du -sh 2>/dev/null | sort -rh | head -20

One environment with a scientific stack in it is commonly 1 to 3 GB on its own. Twelve projects is a serious number, and most of those projects have not been opened in a year.

du -sh ~/Library/Caches/pip

Clearing each one

# The package cache. Always safe.
pip cache purge

# Bytecode, everywhere under the current folder
find . -type d -name "__pycache__" -prune -exec rm -rf {} +

# One project's environment, when you are finished with the project
rm -rf path/to/project/.venv

That is the check worth making before a bulk delete: an environment beside a lock file is disposable, and one without is worth keeping until you write the file.

Why this keeps happening

Nothing in the Python tooling ages an environment out. A project abandoned two years ago keeps its full environment until someone deletes the folder, and because it lives inside the project rather than in a central cache, no cleanup command knows about it.

The same shape appears across every ecosystem: node_modules per project is the identical problem with a different name, and both are usually beaten by container images on machines that use them.

For the order to work through all of it, see the safest order to free 100 GB.

Common questions

Is it safe to delete a Python venv folder?

Yes, provided the project records its dependencies in a requirements.txt, pyproject.toml or lock file, because then pip install rebuilds it. An environment with packages installed by hand and never recorded is not reproducible, and deleting it loses the setup rather than just the download.

How do I clear the pip cache on a Mac?

Run pip cache purge. The cache lives in ~/Library/Caches/pip and holds downloaded packages, not installed ones, so clearing it never breaks an environment. The only cost is that the next install downloads again.

Can I delete __pycache__ folders?

Yes. They hold compiled bytecode that Python regenerates on the next run. Removing them costs one slightly slower start and nothing else. They are also worth adding to .gitignore if they are not already there.

Read next