From what I understand, a cache is an encrypted file of similar files.
What do we do with the __pycache__
folder? Is it what we give to people instead of our source code? Is it just my input data? This folder keeps getting created, what it is for?
First finds all __pycache__ folders in current directory. Execute rm -r {} + to delete each folder at step above ( {} signify for placeholder and + to end the command)
Suppressing the creation of __pycache__ When using the CPython interpreter (which is the original implementation of Python anyway), you can suppress the creation of this folder in two ways. Alternatively, you can set PYTHONDONTWRITEBYTECODE environment variable to any non-empty string.
When a Python source file (module) is imported during an execution for the first time, the appropriate . pyc file is created automatically. If the same module is imported again, then the already created . pyc file is used.
When you run a program in Python, the interpreter compiles it to bytecode first (this is an oversimplification) and stores it in the __pycache__
folder. If you look in there you will find a bunch of files sharing the names of the .py
files in your project's folder, only their extensions will be either .pyc
or .pyo
. These are bytecode-compiled and optimized bytecode-compiled versions of your program's files, respectively.
As a programmer, you can largely just ignore it... All it does is make your program start a little faster. When your scripts change, they will be recompiled, and if you delete the files or the whole folder and run your program again, they will reappear (unless you specifically suppress that behavior).
When you're sending your code to other people, the common practice is to delete that folder, but it doesn't really matter whether you do or don't. When you're using version control (git
), this folder is typically listed in the ignore file (.gitignore
) and thus not included.
If you are using CPython (which is the most common, as it's the reference implementation) and you don't want that folder, then you can suppress it by starting the interpreter with the -B flag, for example
python -B foo.py
Another option, as noted by tcaswell, is to set the environment variable PYTHONDONTWRITEBYTECODE
to any value (according to Python's man page, any "non-empty string").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With