__file__ access extremely slow

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Jun 5 11:15:43 CEST 2009

En Fri, 05 Jun 2009 00:12:25 -0300, John Machin <sjmachin at lexicon.net>  

>> > (2) This will stop processing on the first object in sys.modules that
>> > doesn't have a __file__ attribute. Since these objects aren't
>> > *guaranteed* to be modules,
> Definitely not guaranteed to be modules. Python itself drops non-modules  
> in
> there! Python 2.3 introduced four keys mapped to None -- one of these was
> dropped in 2.4, but the other three are still there in 2.5 and 2.6:

In case someone wonders what all those None are: they're a "flag" telling  
the import machinery that those modules don't exist (to avoid doing a  
directory scan over and over, because Python<2.7 attempts first to do a  
relative import, and only if unsuccessful attempts an absolute one)

> C:\junk>\python23\python -c "import sys; print [k for (k, v) in
> sys.modules.items() if v is None]"
> ['encodings.encodings', 'encodings.codecs', 'encodings.exceptions',
> 'encodings.types']

In this case, somewhere inside the encodings package, there are statements  
like "import types" or "from types import ...", and Python could not find  
types.py in the package directory.

Gabriel Genellina

More information about the Python-list mailing list