[Python-Dev] Importing .pyc in -O mode and vice versa

Josiah Carlson jcarlson at uci.edu
Mon Nov 6 17:36:14 CET 2006


Armin Rigo <arigo at tunes.org> wrote:
> Hi Martin,
> On Sat, Nov 04, 2006 at 04:47:37PM +0100, "Martin v. L?wis" wrote:
> > Patch #1346572 proposes to also search for .pyc when OptimizeFlag
> > is set, and for .pyo when it is not set. The author argues this is
> > for consistency, as the zipimporter already does that.
> 
> My strong opinion on the matter is that importing a .pyc file if the .py
> file is not present is wrong in the first place.  It caused many
> headaches in several projects I worked on.
> 
> Typical example: someone in the project removes a .py file, and checks
> in this change; someone else does an 'svn up', which kills the .py in
> his working copy, but not the .pyc.  These stale .pyc's cause pain, e.g.
> by shadowing the real module (further down sys.path), or simply by
> preventing the project's developers from realizing that they forgot to
> fix some imports.  We regularly had obscure problems that went away as
> soon as we deleted all .pyc files around, but I cannot comment more on
> that because we never really investigated.  

I had a very similar problem the other week when mucking about with a
patch to ntpath .  I had it in a somewhat small temporary projects
folder and needed to run another project.  It picked up the local
ntpath.py when importing path.py, but then failed because I was working
on a 2.5 derived ntpath, but I was using 2.3 to run the other project. 
After renaming the local ntpath, I continued to get the error until I
realized "damn pyc" and was halfway through a filsystem wide search for
the problem code (10 minutes elapsed).

About the only place where I have found the need for pyc-without-py
importing is for zipimports, specifically as used by py2exe and other
freezing applications.  I don't know if we want to add a new command
line option, or a __future__ import, or something, but I think there
should be some method of warning people that an import was performed
without source code.


 - Josiah



More information about the Python-Dev mailing list