Overview
Thermpyc command recursively searches the project directory for any __pycache__ directories and deletes them along with their contents. This is useful for cleaning up compiled Python files that may be causing issues or taking up unnecessary space.
What It Does
The command:- Scans the project’s package and tests directories for
__pycache__directories - Recursively searches all subdirectories
- Deletes each
__pycache__directory and all.pycfiles within - Reports each deletion to stdout
Usage
Basic Usage
With Verbose Output
Quiet Mode
Expected Output
What Gets Deleted
The command targets:- All
__pycache__/directories - All
.pycfiles (compiled bytecode) - All
.pyofiles (optimized bytecode)
Directory Structure Before
Directory Structure After
__pycache__/ directories are gone.
Behavior
Scans both the main package (from
pyproject.toml) and the tests package.Recursively searches all subdirectories using
Path.rglob("__pycache__").Deletes directories immediately using
shutil.rmtree(). No confirmation prompt.Safe to run multiple times. Subsequent runs find no directories to delete.
Prints each deletion to stdout using
typer.echo().When to Use
Use rmpyc When:
- Import errors - Stale
.pycfiles causing module import issues - Debugging - Ensuring you’re running the latest source code
- Disk space - Cleaning up unnecessary compiled files
- Before deployment - Cleaning the project before packaging
- After refactoring - Removing cached files from renamed/deleted modules
- Version control - Cleaning up before committing (though
.pycshould be git-ignored)
Common Scenarios
Stale Bytecode Issues
After Major Refactoring
Disk Space Cleanup
What About .pyc Files?
Python automatically creates.pyc files when:
- A module is imported
- Python compiles the source code to bytecode
- The bytecode is cached for faster subsequent imports
When .pyc Files Cause Problems
- Renamed files: Old
.pycfiles can cause import errors - Moved modules: Cached paths become stale
- Modified code: Sometimes Python doesn’t detect changes (rare)
- Python version changes:
.pycfiles from different Python versions
Python usually handles
.pyc invalidation automatically. You only need rmpyc when something goes wrong or for cleanup.Alternative Approaches
| Approach | Command | Scope |
|---|---|---|
| pyrig | uv run pyrig rmpyc | Project package + tests |
| find | find . -type d -name __pycache__ -exec rm -rf {} + | Entire directory tree |
| Python | python -Bc "import compileall" | Disable .pyc creation |
| git clean | git clean -fdX | All git-ignored files |
Integration with Other Commands
Before Running Tests
After Code Changes
Before Building
Performance
The command is fast even on large projects:- Uses
Path.rglob()for efficient filesystem traversal - Deletes directories in a single pass
- Minimal overhead from pyrig CLI
Safety
Python regenerates
__pycache__ directories automatically when modules are imported.Only deletes compiled bytecode, never source code or data files.
No confirmation prompt - deletion is immediate. Use with awareness.
While safe in practice, always ensure you’re in the correct directory before running bulk deletion commands.
Troubleshooting
Permission Errors
Still Seeing Import Issues
Ifrmpyc doesn’t fix import problems:
- Check Python version: Ensure you’re using the correct Python version
- Verify environment: Make sure virtual environment is activated
- Check sys.path: Verify import paths are correct
- Reinstall package: Try
uv syncto reinstall dependencies
Git Integration
Python cache files should be git-ignored:__pycache__:
Related Commands
- init - Generates
.gitignorethat excludes__pycache__ - mkroot - Updates
.gitignoreconfiguration - build - Building (often done after cleanup)
Implementation
Thermpyc command:
- Gets package names from
PackageManager.I.package_name()andProjectTester.I.tests_package_name() - Converts package names to
Pathobjects - Uses
Path.rglob("__pycache__")to find all cache directories - Deletes each with
shutil.rmtree() - Prints each deletion with
typer.echo()
pyrig/rig/cli/commands/remove_pycache.py:16.
Run
uv run pyrig rmpyc --help to see the command’s built-in help text.