How to prevent python from using orphaned .pyc files? (ones with no matching .py files)

bytecode, pyc, python

Solution

Try this (see here):

`PYTHONDONTWRITEBYTECODE`

If this is set, Python won’t try to write `.pyc` or `.pyo` files on the import of source modules. This is equivalent to specifying the `-B` option.

New in version 2.6.

But there is one issue associated with it: you will loose the benefit of having `.pyc` files (thus losing performance). The better, cleaner and friendlier way is to walk through the directory and clean orphaned `.pyc` files you do not need. You should do it with a script and be sure you do not have `.pyc` files that are not meant to be associated with `.py` ones (eg. for some level of obfuscation).

Problem

Once in a while I run into a very difficult-to-debug problem: there's a leftover .pyc file somewhere in my $PYTHONPATH, and the matching .py file has been moved to somewhere else that's later in $PYTHONPATH - so when I try to import the module, the "orphaned" .pyc file is used and all changes to the "real" .py file are ignored, leaving me incredibly confused until I figure out that's what's happening. Is there any way of making python not use "orphaned" .pyc files, or print a warning when using them? Alternatively, does the fact that I have this problem at all mean I'm doing something wrong, and if so, what?

Original source