How to remove __pycache__ folders and .pyc files

How to remove __pycache__ folders and .pyc files

These folders and files are generated by python. You should add these to your VCS’s ignore file. But if you need to clean these manually, you can do with the command below:

find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

This command will remove all files that end with .pyc and __pycache__ folder recursively in the current directory. If you are using this command a lot, you should think about adding it to your shell alias list.

Note: For Python 3, you only need to remove __pycache__ folder because .pyc files are inside them.

Alternative: You can prevent the generation of the file in the development using the environment variable below:

export PYTHONDONTWRITEBYTECODE=""

References

  1. https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE