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?
Python can now be prevented from writing . pyc or . pyo files by supplying the -B switch to the Python interpreter, or by setting the PYTHONDONTWRITEBYTECODE environment variable before running the interpreter. This setting is available to Python programs as the sys.
. py files contain the source code of a program. Whereas, . pyc file contains the bytecode of your program.
pyc file is created by the Python interpreter when a *. py file is imported into any other python file. The *. pyc file contains the “compiled bytecode” of the imported module/program so that the “translation” from source code to bytecode can be skipped on subsequent imports of the *.
A program doesn't run any faster when it is read from a ". pyc" or ". pyo" file than when it is read from a ". py" file; the only thing that's faster about ".
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).
You cannot prevent Python from loading .pyc files if it finds them in it's path, no.
You have a few options:
Import the offending module and figure out what it's path is:
>>> import borkenmod
>>> import sys
>>> sys.modules[borkenmod.__name__].__file__
'lib/python2.7/borkenmod.pyc'
Use a script to walk your PYTHONPATH and delete stale bytecode files. The linked script removes all .pyc
files for which there is no corresponding .py
file present.
Wipe all .pyc files in your PYTHONPATH, and have Python regenerate them with the compileall
module:
python -m compileall [path]
then remove that one file.
Do note that the last two options could result in the removal of legitimate python modules! Some python modules (especially commercially licenced) are distributed as compiled bytecode only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With