Package: pyclean
pycleanWorried about.pycfiles 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 thePython 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 userm**/*.pycorfind-name'*.py?'-delete?If you’re happy withrmorfind, go for it! When I waslooking
for a simple, concise solution for everybodyI figured people are
struggling, and simple things are more complicated than they appear at
first sight.Also, there is apycleancommand (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!)DebianJust for reference, the Python scripts Debian ships with itspython-minimalandpython3-minimalpackages can be found at:pyclean:salsa.debian.org/cpython-team/python-defaultspy3clean:salsa.debian.org/cpython-team/python3-defaultspypyclean:salsa.debian.org/debian/pypyInstallation$pipinstallpycleanUsage$pyclean--helpIf you want to explicitly operate the Debian-specific implementation:$py2clean--help$py3clean--help$pypyclean--helpClean up all bytecode in the current directory tree, and explain verbosely:$pyclean-v.Clean up all bytecode for a Debian package: (may require root permissions)$pyclean-ppython3-keyring--legacyClean up debrisPyClean 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)Jupyter (notebook checkpoints) –optionalMypy (mypy cache folder) –optionalRuff (ruff cache folder) –optionalTox (tox environments) –optionalExample:Dry-run a cleanup of bytecode and tool debris in verbose mode
(to see what would be deleted):$pyclean.--debris--verbose--dry-runRemove arbitrary file system objectsPyClean also lets you remove free-form targets using globbing. Note that
this ispotentially dangerous: You can delete everything anywhere in
the file system, including the entire project you’re working on. For this
reason, the--eraseoption 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 add the--yesoption, in addition.$pyclean.--erasetmp/**/*tmp/The above would delete the entiretmp/directory with all subdirectories
inside the current folder. If you omit the finaltmp/you’ll leave the
emptytmpdirectory in place. (WARNING!Don’t put the.afterthe--eraseoption! Obviously, your project files will all be deleted.)Use pyclean with ToxIf you want to avoid installingpycleanyou can add it to yourtox.inifile as follows:[testenv:clean]skip_install=truedeps=pycleancommands=pyclean {posargs:. --debris}You’ll then be able to run it withToxlike this:$tox-ecleanDevelopmentIf you want to help out please see ourcontribution guide.Roadmap (for v3.0.0)Replace original Debian scripts (current--legacy) by a single,
pure Python, Python 3-only code base that serves all target platforms.Reduce the package dependencies to an absolute minimum for maximum
portability.Add additional CLI options to delete debris from builds, testing and
packaging (build/, .cache/, dist/, .pytest_cache/, .tox/ and
free-form targets).