__pycache__
is a folder containing Python 3 bytecode compiled and ready to be executed.
I don't recommend routinely deleting these files or suppressing creation during development as it may hurt performance. Just have a recursive command ready (see below) to clean up when needed as bytecode can become stale in edge cases (see comments).
Python programmers usually ignore bytecode. Indeed __pycache__
and *.pyc
are common lines to see in .gitignore
files. Bytecode is not meant for distribution and can be disassembled using dis
module.
If you are using OS X you can easily hide all of these folders in your project by running following command from the root folder of your project.
find . -name '__pycache__' -exec chflags hidden {} \;
Replace __pycache__
with *.pyc
for Python 2.
This sets a flag on all those directories (.pyc files) telling Finder/Textmate 2 to exclude them from listings. Importantly the bytecode is there, it's just hidden.
Rerun the command if you create new modules and wish to hide new bytecode or if you delete the hidden bytecode files.
On Windows the equivalent command might be (not tested, batch script welcome):
dir * /s/b | findstr __pycache__ | attrib +h +s +r
Which is same as going through the project hiding folders using right-click > hide...
Running unit tests is one scenario (more in comments) where deleting the *.pyc
files and __pycache__
folders is indeed useful. I use the following lines in my ~/.bash_profile
and just run cl
to clean up when needed.
alias cpy='find . -name "__pycache__" -delete'
alias cpc='find . -name "*.pyc" -delete'
...
alias cl='cpy && cpc && ...'
and more lately
# pip install pyclean
pyclean .