Tuesday, 5 February 2013

Removing .pyc Files: A Coda

A few days ago, I wrote a blog post detailing a git hook that would automatically remove .pyc files when checking out a different branch. I received a variety of feedback, which I will outline here.

Firstly, +Jeff Mahoney provided a more efficient implementation of the git hook in a comment on the original post. I haven't tested this, but it might provide a more efficient implementation if you need it.

Secondly, there are a number of helpful comments on the reddit post, including a fine-tuned git/xargs command.

Finally, and most importantly, a number of people (both in the blog comments and on reddit) pointed out the PYTHONDONTWRITEBYTECODE environment variable.  Setting this to anything will mean that Python doesn't generate .pyc or .pyo files, completely circumventing the problem that I was trying to solve.


  1. So what about the performance penalty of not having pyc files?

    1. This is purely for development purposes; I would absolutely not suggest using PYTHONDONTWRITEBYTECODE in production.

      And having said that, our test run (of a little more than 2 minutes) demonstrates no run-time difference with the environment variable set, so I suspect you're unlikely to notice.

    2. There shouldn't be a runtime difference, as far as I understand it. The python runtime generates the bytecode if a .pyc doesn't exist, so the end result is the same (with a small startup delay)

    3. Then it's ok for development, very handy the tip btw, I'll use it in my repos.

  2. FWIW, Ubuntu provides a "pyclean" command. The command, at /usr/bin/pyclean, is the source code.