What is __pycache__?
__pycache__
is a directory that is created by the Python interpreter when it imports a module. It contains the compiled bytecode of the module, which can be used to speed up subsequent imports of the same module. The bytecode is specific to the version of Python that was used to generate it, so different versions of Python will create different bytecode files.
Here is a code snippet that demonstrates how __pycache__
is created:
import my_module
# The first time this is run, the interpreter will import my_module, and create a __pycache__ directory
# in the same directory as my_module.py
import my_module
# The second time this is run, the interpreter will look for a compiled version of my_module in the
# __pycache__ directory, and use that instead of re-compiling the source code.
It's worth noting that this only happens when python is running in a version greater than 3.2 and in development mode. In other words, if the python process is invoked with the -OO option, then pycache won't be created.