What is the best way to clear out all the __pycache__ folders and .pyc/.pyo files from a python3 project?
__pycache__
folders and their associated .pyc
or .pyo
files are automatically generated by Python to cache bytecode for faster module imports. If you want a completely “clean” project environment—perhaps before distribution or a fresh start—you can remove these files and directories without any issues. Below are a few methods:
1. Using find
and rm
Commands (Linux/macOS)
In Unix-like systems, the most common approach is to use the find
command in combination with rm
:
# Remove all __pycache__ folders find . -type d -name "__pycache__" -exec rm -r {} + # Remove all .pyc and .pyo files find . -type f -name "*.pyc" -exec rm -f {} + find . -type f -name "*.pyo" -exec rm -f {} +
-type d
restricts the search to directories.-type f
restricts the search to files.-exec rm -r {}
recursively removes the matched directory{}
.+
at the end groups multiple matches in fewer commands, improving efficiency.
Tip: Use -exec rm -iv {}
if you’d like to be prompted for each removal, or skip the -i
to remove them without confirmation.
2. Using a Single Find Command (Combined)
You can also combine multiple conditions into one find
command:
find . \( -name "__pycache__" -o -name "*.pyc" -o -name "*.pyo" \) -exec rm -rf {} +
\( -name "__pycache__" -o -name "*.pyc" -o -name "*.pyo" \)
means “match directories or files whose name is__pycache__
,*.pyc
, or*.pyo
.”-exec rm -rf {}
will remove those files or directories without additional prompts (use caution!).
3. Using pyclean
(Debian-Based Systems)
If you’re on a Debian-based system like Ubuntu, there’s a pyclean
utility installed with python3-minimal
. It helps remove cached bytecode files:
pyclean .
This scans your current directory for Python bytecode files and removes them. It doesn’t need complex parameters, making it a straightforward choice if available on your system.
4. Using a Python Script
You can also write a small Python script to recursively delete __pycache__
directories and .pyc
files. Here’s a simple snippet:
import os import sys def remove_pycaches_and_pycs(directory): for root, dirs, files in os.walk(directory): # remove __pycache__ directories if "__pycache__" in dirs: pycache_dir = os.path.join(root, "__pycache__") print(f"Removing directory: {pycache_dir}") os.rmdir(pycache_dir) # or use shutil.rmtree(pycache_dir) # remove .pyc and .pyo files for file in files: if file.endswith(".pyc") or file.endswith(".pyo"): file_path = os.path.join(root, file) print(f"Removing file: {file_path}") os.remove(file_path) if __name__ == "__main__": directory_to_clean = sys.argv[1] if len(sys.argv) > 1 else "." remove_pycaches_and_pycs(directory_to_clean)
Pros:
- You can integrate it into your project as a custom cleanup step in CI/CD or local scripts.
- You can add more logic or options (e.g., verbose mode, logging).
Cons:
- Slightly more verbose than one-line shell commands.
- Requires Python to be installed (which is typically the case in a Python project).
5. Using .gitignore
or Similar Mechanisms
While this doesn’t remove files per se, it’s good practice to exclude __pycache__
folders and .pyc
files from version control so they’re never committed. For Git:
# .gitignore __pycache__/ *.pyc *.pyo
This ensures these files don’t clutter up your repository. You can still generate and remove them locally as needed.
Final Thoughts
- Preferred Method: In most cases, the
find ... rm
approach is the fastest and most widely used. - Automation: Consider creating a small script or alias in your development environment (
cleanpy
) to quickly run these commands. - Safety: Always review which files/directories will be removed (e.g., by using
-print
withfind
) before running the final command. This helps avoid accidentally deleting important data.
By regularly cleaning up bytecode artifacts, you keep your Python project’s workspace lean and avoid confusion during deployment or distribution.