1. Importing a Module
When you import a module in Python, the interpreter looks for the source code file (with a .py
extension) corresponding to the module.
import mymodule
2. Compilation to Bytecode
If the module is found, the Python interpreter compiles the source code into bytecode. Bytecode is a set of instructions for the Python virtual machine that’s more efficient to execute than the original source code.
3. Storing Bytecode in __pycache__
The compiled bytecode is then saved in a file with the .pyc
extension inside a __pycache__
directory located in the same directory as the original .py
file. The .pyc
file’s name usually contains the Python version number to ensure compatibility.
For example, if you have a module named mymodule.py
and you’re using Python 3.8, the compiled bytecode might be stored in a file like __pycache__/mymodule.cpython-38.pyc
.
4. Reusing Compiled Modules
The next time you import the same module, Python will check the __pycache__
directory for the corresponding .pyc
file. If the .pyc
file exists and the source code hasn’t changed since the .pyc
file was created, Python will use the bytecode from the .pyc
file instead of recompiling the source code. This can speed up the import process.
Summary
The __pycache__
directory is a key part of Python’s strategy for speeding up program execution. By compiling modules into bytecode and storing the results in .pyc
files within __pycache__
, Python can avoid the overhead of recompiling modules every time they are imported. This mechanism is mostly transparent to the programmer but is a vital part of what makes Python a convenient and efficient language to use.