Pure Python cross-platform pyclean. Clean up your Python bytecode.
Project description
pyclean
Worried about .pyc files and __pycache__ directories? Fear not! PyClean is here to help. Finally the single-command clean up for Python bytecode files in your favorite directories. On any platform.
Presented at PyConX, Firenze 2019.
Wait! What is bytecode?
Bytecode is opcodes for the Python Virtual Machine. – Confused?
If you want to deep-dive into the topic watch the 2013 EuroPython talk “All Singing All Dancing Python Bytecode” by Larry Hastings. Otherwise James Bennett’s “Introduction to Python bytecode” should provide you with just the sound understanding of what it is all about.
Why not simply use rm **/*.pyc or find -name '*.py?' -delete?
If you’re happy with rm or find, go for it! When I was looking for a simple, concise solution for everybody I figured people are struggling, and simple things are more complicated than they appear at first sight.
Also, there is a pyclean command (and its siblings) on Debian. And, well, only on Debian as it turns out. Not that I’m a big fan of Mircosoft Windos, but why ignore the biggest Python population on this planet? (As if they weren’t punished enough already using this unfree piece of software!)
Debian
Just for reference, the Python scripts Debian ships with its python-minimal and python3-minimal packages can be found at:
pypyclean: salsa.debian.org/debian/pypy
Installation
$ pip install pyclean
or
$ python -m pip install pyclean
Usage
$ pyclean --help
or
$ python -m pyclean --help
Clean up all bytecode in the current directory tree, and explain verbosely:
$ pyclean -v .
Clean up debris
PyClean can clean up leftovers, generated data and temporary files from popular Python development tools in their default locations, along with Python bytecode. The following topics are currently covered:
Cache (general purpose folder for several tools, e.g. Python eggs, legacy Pytest)
Coverage (coverage database, and supported file formats)
Packaging (build files and folders)
Pytest (build files and folders)
Ruff (ruff cache folder)
Jupyter (notebook checkpoints) – optional
Mypy (mypy cache folder) – optional
Tox (tox environments) – optional
Example: Dry-run a cleanup of bytecode and tool debris in verbose mode (to see what would be deleted):
$ pyclean . --debris --verbose --dry-run
Remove arbitrary file system objects
PyClean also lets you remove free-form targets using globbing. Note that this is potentially dangerous: You can delete everything anywhere in the file system, including the entire project you’re working on. For this reason, the --erase option has a few artificial constraints:
It doesn’t do recursive deletion by itself, which means that you have to specify the directory and its contents, separately and explicitly.
The above entails that you’re responsible for the deletion order, i.e. removal of a directory will only work if you asked to delete all files inside first.
You’re prompted interactively to confirm deletion, unless you specify the --yes option, in addition.
$ pyclean . --erase tmp/**/* tmp/
The above would delete the entire tmp/ directory with all subdirectories inside the current folder. If you omit the final tmp/ you’ll leave the empty tmp directory in place. (WARNING! Don’t put the . after the --erase option! Obviously, your project files will all be deleted.)
Use pyclean with Tox
If you want to avoid installing pyclean you can add it to your tox.ini file as follows:
[testenv:clean]
skip_install = true
deps = pyclean
commands = pyclean {posargs:. --debris}
You’ll then be able to run it with Tox like this:
$ tox -e clean
Development
If you want to help out please see our contribution guide.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyclean-3.0.0.tar.gz
.
File metadata
- Download URL: pyclean-3.0.0.tar.gz
- Upload date:
- Size: 47.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a48be7afd512b1923a7b9decc1219bbbe2d73199c939b46c936448f904a180ce |
|
MD5 | e278686c01d1fc7dcac7ae5bc1de1657 |
|
BLAKE2b-256 | 007135ccfe48e762a40217ef8052a13c23e3f068d61453742813ca4ee2a41938 |
File details
Details for the file pyclean-3.0.0-py3-none-any.whl
.
File metadata
- Download URL: pyclean-3.0.0-py3-none-any.whl
- Upload date:
- Size: 34.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1d8114991cf09994bc511cd0b050f3e398ae2bc8bfc0bbd1bdbec8ba048e6dd |
|
MD5 | fc48db1a375a00a05a9b3038fc2cd254 |
|
BLAKE2b-256 | d8811d4e0bc85dabe0a6eec62c91939eea6c96d9ab7113d983250e0dab0b20ef |