Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 259
weronika Avatar asked May 28 '12 18:05

weronika


People also ask

How do you stop a .PYC file?

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.

What is the difference between .py and .PYC files?

. py files contain the source code of a program. Whereas, . pyc file contains the bytecode of your program.

Why .PYC file is created in python?

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 *.

Does PYC run faster than py?

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 ".


2 Answers

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).

like image 174
zizozu Avatar answered Oct 13 '22 17:10

zizozu


You cannot prevent Python from loading .pyc files if it finds them in it's path, no.

You have a few options:

  1. 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'
    
  2. 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.

  3. 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.

like image 44
Martijn Pieters Avatar answered Oct 13 '22 16:10

Martijn Pieters