Understanding pycache in Python: Everything You Need to Know
Updated on
If you've ever run a Python script, you might have noticed a folder named __pycache__ appearing inside your project. It may look unimportant, but it plays a critical role in how Python executes your programs efficiently. In this guide, we’ll break down what the __pycache__ folder is, why Python generates it, and how you can manage or remove it safely.
Python is an interpreted language, but it doesn’t re-interpret every .py file from scratch each time you run it. Instead, Python compiles your source code into bytecode—a lightweight, low-level representation that can be executed more quickly. These compiled bytecode files (with the .pyc extension) are stored inside the __pycache__ folder.
Want to quickly create Data Visualization from Python Pandas Dataframe with no code?
PyGWalker is a Python library for Exploratory Data Analysis with Visualization.
PyGWalker (opens in a new tab) turns your pandas or polars dataframe into a Tableau-like interface directly inside Jupyter Notebook.
What is pycache in Python?
The __pycache__ folder is automatically created when Python runs a module. Inside it, Python stores .pyc files—a compiled, bytecode version of your .py file.
Bytecode is designed for faster execution. When you run a script, Python looks for an up-to-date .pyc file:
- If it exists and matches the current source code → Python loads the
.pycfile immediately. - If not → Python recompiles the source code, generates a new
.pycfile, and stores it in__pycache__.
Why the file names look like module.cpython-310.pyc
Modern Python includes interpreter tagging (e.g., cpython-311) so multiple Python versions can co-exist without conflicts.
This is especially important when using:
- Virtual environments
- System Python + environment-specific Python
- Tools like pyenv, conda, or Docker images
Where is the pycache folder located?
The __pycache__ folder appears in the same directory as the Python module being run.
Example structure:
my_project/
main.py
utils.py
**pycache**/
main.cpython-311.pyc
utils.cpython-311.pyc
If you import a module from another directory, that directory will also generate its own __pycache__.
In virtual environments
Virtual environments often contain many __pycache__ folders inside installed packages. This is normal—package imports generate .pyc files on first use.
How does pycache speed up Python scripts?
Running a Python script normally requires:
- Parsing the text
- Building an Abstract Syntax Tree (AST)
- Compiling the AST to bytecode
- Executing the bytecode
Steps 1–3 are skipped when Python finds an up-to-date .pyc file. This is why:
- First run may be slightly slower
- Subsequent runs are faster (especially in large codebases)
Hash-based pyc files (PEP 552)
Modern Python supports both:
- Timestamp-based
.pyc(default) - Hash-based
.pyc(for reproducible builds, packaging, Docker images)
This makes bytecode generation more stable in automated or containerized environments.
Can I delete the pycache folder?
Yes — deleting __pycache__ is safe.
Python will recreate it automatically the next time you run your scripts. The only downside: your next execution may be slightly slower due to recompilation.
To remove all __pycache__ folders recursively:
find . -type d -name __pycache__ -exec rm -r {} \+Common use cases for deleting pycache
- Debugging import issues
- After renaming/relocating modules
- When packaging for deployment
- To reduce noise before a Git commit or Docker build
How do I disable the creation of pycache files?
You can disable .pyc creation by setting the environment variable:
export PYTHONDONTWRITEBYTECODE=1When enabled:
- Python will not generate any
.pycfiles __pycache__folders will not be created
This is commonly used in:
- Docker images (to reduce layer size)
- CI pipelines
- Small scripts where performance is irrelevant
To make this setting permanent, add the variable to your shell configuration (e.g., ~/.bashrc).
How to Ignore pycache Files in Git
Since .pyc files are machine-specific and auto-generated, they should not be stored in version control.
Add this to your .gitignore:
__pycache__/
*.pycThe *.pyc rule is optional but helpful when legacy Python versions place .pyc next to .py.
What Happens If I Delete the pycache Folder?
Nothing breaks. Your script will run normally.
Python will simply:
- Detect the missing
.pyc - Recompile the
.pyfile - Recreate the
__pycache__directory and.pycfile
Only the first run after deletion is slightly slower.
FAQ
1. What is the purpose of the __pycache__ folder in Python?
The folder stores compiled .pyc bytecode files, which make Python start faster on subsequent runs.
2. Can I delete the __pycache__ folder?
Yes. Deleting it is safe. Python regenerates it automatically.
3. How can I ignore __pycache__ files in Git?
Add this to your .gitignore:
__pycache__/
*.pyc