Remove __pycache__ folders and .pyc files in Python Project

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
4 min

banner

# Remove __pycache__ folders and .pyc files in Python Project

To remove the __pycache__ folders and .pyc files in a Python project:

  1. Open your terminal in your project's root directory.
  2. Issue the following command.
shell
find . | grep -E "(/__pycache__$|\.pyc$|\.pyo$)" | xargs rm -rf

remove pycache folders and pyc files in python project

The command uses a regular expression to find and remove the __pycache__ directories, .pyc and .pyo files.

The command removes the __pycache__ directories and .pyc files, recursively, so make sure to open your terminal in your project's root directory before running it.

If you are on Linux, you can also use the find command with a regular expression.

shell
find . -regex '^.*\(__pycache__\|\.py[co]\)$' -delete

using the find command with a regular expression

# Using the -name parameter to delete __pycache__ and .pyc files

You can also use the -name parameter to delete the __pycache__ directories and the .pyc files.

  1. Open your terminal in your project's root directory.
  2. Issue the following command.
shell
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete

using the name parameter to delete the files

You can also try the following command for deleting all __pycache__ directories.

shell
find . -type d -name __pycache__ -exec rm -r {} \+

delete all pycache directories

# Adding the command to your ~/.bashrc or ~/.zshrc file

If you have to do this often, add the command to your ~/.bashrc or ~/.zshrc profile file.

For example, you can edit your ~/.bashrc file by using one of the following commands.

shell
# with `gedit` sudo gedit ~/.bashrc # or with `nano` sudo nano ~/.bashrc

And you can edit your ~/.zshrc profile file by issuing one of the following commands.

shell
sudo gedit ~/.zshrc sudo nano ~/.zshrc

Once you open the profile file in your preferred text editor, add the following command to it.

~/.bash_rc
rm-pycache () { find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete }

Save the file and close it.

Anytime you make changes to your profile file, you have to source it.

shell
# BASH source ~/.bashrc source ~/.bash_profile # ZSH source ~/.zshrc

Once you've sourced your profile file (or restarted your terminal), you can use the rm-pycache command.

  1. Open your terminal in your project's root directory.
  2. Issue the rm-pycache command.
shell
rm-pycache

use the rm pycache command

You can name the command as you see fit, it doesn't have to be named rm-pycache.

However, make sure to source your profile file anytime you make changes to it.

# Using the python interpreter to delete all __pycache__ folders and .pyc files

You can also use the python interpreter to delete all __pycache__ folders and .pyc files.

  1. Open your terminal in your project's root directory.
  2. Issue the following commands.
shell
python -Bc "import pathlib; [p.unlink() for p in pathlib.Path('.').rglob('*.py[co]')]" python -Bc "import pathlib; [p.rmdir() for p in pathlib.Path('.').rglob('__pycache__')]"

using python interpreter to delete pycache and pyc files

Depending on your Python installation, you might have to use python3 instead of python.

shell
python3 -Bc "import pathlib; [p.unlink() for p in pathlib.Path('.').rglob('*.py[co]')]" python3 -Bc "import pathlib; [p.rmdir() for p in pathlib.Path('.').rglob('__pycache__')]"

If you are on Windows, you might have to use the py alias.

shell
py -Bc "import pathlib; [p.unlink() for p in pathlib.Path('.').rglob('*.py[co]')]" py -Bc "import pathlib; [p.rmdir() for p in pathlib.Path('.').rglob('__pycache__')]"

The first command finds and deletes all .pyc and .pyo files and the second deletes all __pycache__ directories.

# Using the py3clean command to delete __pycache__ and .pyc files

You can also use the py3clean command to delete __pycache__ directories and .pyc files.

  1. Open your terminal in your project's root directory.
  2. Run the py3clean . command.
shell
py3clean .

run py3clean command

You can also try to use the pyclean module.

First, install the module by running the following command.

shell
pip install pyclean # Or with pip3 pip3 install pyclean

install pyclean module

Open your terminal in your project's root directory and run the following command.

shell
pyclean -v .

delete pycache folders and pyc files using pyclean

The -v flag stands for "verbose".

You can remove the flag if you don't want to get the output in verbose mode.

shell
pyclean .

# Setting the PYTHONPYCACHEPREFIX environment variable

Since Python 3.8, you can also set the PYTHONPYCACHEPREFIX environment variable.

If the environment variable is set, Python writes .pyc files in a mirror directory tree at the specified path, instead of in __pycache__ directories within the source tree.

For example, on macOS and Linux, you could add something like this to your profile file(~/.bashrc or ~/.zshrc).

~/.bashrc
export PYTHONPYCACHEPREFIX="$HOME/.cache/pycache"

Make sure the specified directory exists.

Now, Python won't write your .pyc files in a __pycache__ directory within the source tree.

Instead, your .pyc files will be in a mirror directory tree at the specified path.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.