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

"Martin v. Löwis" martin at v.loewis.de
Thu Nov 9 06:11:13 CET 2006

Greg Ewing schrieb:
> Martin v. Löwis wrote:
>> a) the directory cache is out of date, and you should
>>    re-read the directory
>> b) the module still isn't there, but is available in
>>    a later directory on sys.path (which hasn't yet
>>    been visited)
>> c) the module isn't there at all, and the import will
>>    eventually fail.
>> How can the interpreter determine which of these it
>> is?
> It doesn't need to - if there is no file for the module
> in the cache, it assumes that the cache could be out
> of date and rebuilds it. If that turns up a file, then
> fine, else the module doesn't exist.

I lost track. I thought we were talking about creating
a cache of directory listings, not a stat cache?

If you invalidate the cache when a file name is not listed,
you will invalidate it on nearly every import, and multiple
times, too: Python looks for foo.py, foo.pyc, foo.so,
foomodule.so. At most one of them is found, the others
aren't. So if foo.so would be found, are you invalidating
the cache because foo.py isn't?

> BTW, I'm not thinking of cacheing individual directories,
> but scanning all the directories and building a single
> qualified_module_name -> pathname mapping. If the cache
> gets invalidated, all the directories along the path
> are re-scanned, so a new module will be picked up
> wherever it is on the path.

That won't work well with path import objects. You
have to observe the order in which sys.path is
scanned, for correct semantics.


More information about the Python-Dev mailing list