Why Python __pycache__ file? 🤔


At first, I wondered why this file was created automatically. Later, I learned the reason and gained a deeper understanding.
You will never see this __pycache__.pyc
if you are working on top-level files (Single file in the folder).
__pycache__.pyc
is automatically generated at the time of importing code from other files to store compiled byte code files in .pyc
format . When a Python script or module is executed, the interpreter translates the source code into bytecode before executing it.
__pycache__.pyc
It updates only when you make changes or add a new file. This does not change the entire file; it only updates the parts you have modified.
Most online compilers hide __pycache__.pyc
, so you won't see it there.
Bytecode is not machine code; it is a Python-specific interpretation. Machine code consists of direct instructions to the hardware. Bytecode is optimized specifically for Python.
How naming works
hello_chai.cpython-312.pyc
hello_chai: this is a file name we set.
CPython: CPython is the default and most widely used implementation of the Python language.
312: This shows which version we are using as I am using 3.12
Thank you for reading.
Subscribe to my newsletter
Read articles from Sunil kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sunil kumar
Sunil kumar
Always eager to learn and grow, I aim to make a positive impact through my skills.