What is pycache?
If you write code in Python, you have undoubtedly noticed that whenever you run a script, a mysterious __pycache__ folder instantly appears in your directory, cluttering up your workspace. Inside it, you will find files ending in .pyc (Python Compiled).
Why Does It Exist?
Python is an interpreted language, meaning it reads your human-readable source code (.py) and translates it into machine instructions on the fly. This translation takes time.
To speed things up, the first time you run a Python script, the Python interpreter translates your code into intermediate “bytecode” and saves it in the __pycache__ folder as a .pyc file. The next time you run the script, Python completely skips the translation step and just runs the .pyc file, resulting in a noticeably faster startup time.
Can You Delete It?
Yes, it is 100% safe to delete.
The __pycache__ folder contains absolutely no original source code or data. It is purely a performance cache.
How to Delete It (and Stop It)
You can simply right-click and delete the folder at any time.
However, if you want to stop Python from generating these folders entirely (for example, in a small testing environment where you don’t care about the microsecond speed boost), you can set an environment variable in your terminal:
Linux/macOS:
export PYTHONDONTWRITEBYTECODE=1
Windows:
set PYTHONDONTWRITEBYTECODE=1
What Happens If You Delete It?
Nothing breaks. The very next time you execute your Python script, the interpreter will take a fraction of a second longer to translate your source code, and it will silently regenerate a brand new __pycache__ folder to speed up the next run.
Bottom Line
__pycache__ is a harmless, automatically generated performance booster for Python scripts. It is perfectly safe to delete, and you should always ensure it is added to your .gitignore file so you don’t accidentally upload it to GitHub.
Discussion
Loading authentication...