Categories
Python

Django management command: cleanup .pyc files

While distributing a django app, the pyc files could become very annoying. So I wrote this django management command to quickly clean up the *.pyc files from all the directories. The file should be placed under “commands” directory under the “management” directory of an application. So basically the file structure should be like: < appname >/management/commands/rmpyc.py. Note that both the management and commands directories must be python packages, that is they should both have their own __init__.py.

Here is the code:

Usage:
When you have put the application in the “INSTALLED_APPS”, you can now use commands like this to cleanup the python bytecode files:

2 replies on “Django management command: cleanup .pyc files”

Nice exercise to learn django command. However, this should be done via SCM (e.g., git). Have a global ignore file in your system and add *.py{c,o} there. So those files will be ignored for all projects.

Also, just alias the following in .bashrc

alias rmpyc=”find . -name ‘*.pyc’ -exec rm {} \;”

So that, if you ever need to cleanup those cache files, you can run the alias.

Yes vaiya, I personally use the alias in my bash_profile and I add these files to .gitignore right at the beginning. Just wanted to write something to learn the usages of django commands.

Comments are closed.