Non-caching import?
Kevin Smith
Kevin.Smith at sas.com
Mon May 7 11:51:00 EDT 2001
Is it possible to implement an import mechanism that will not use the caching
mechanism? For example, let's say that I have the following file/directory
structure. The only difference between the two structures is that
/home/lib/more.py has an extra class "Blech" defined.
/usr/lib/python/site-packages/:
foo.py:
from more import *
class Bar: pass
more.py:
class Blah: pass
/home/lib/:
foo.py:
from more import *
class Bar: pass
more.py:
class Blah: pass
class Blech: pass
Now we run the following code...
# Import foo from /usr/lib/python/site-packages/.
import foo
# Insert the directory into the path where the other foo.py exists.
sys.path.insert(0, '/home/lib/')
# Import foo again (hopefully from /home/lib/).
import foo
After the second import, there are no changes to foo. Class Bar is still the
class definition from site-packages/foo.py. I can get Bar from the second
import to act the way I would like by doing a "del sys.modules['foo']" after
the first import, but this has no effect on the "from more import *" in
foo.py.
So I end up not having access to class Blech, and Blah is still from
site-packages/foo.py because the caching mechanism blocked them from being
loaded from /home/lib/foo.py. Is there a way around this (possibly using
__import__())?
P.S. Keep in mind that this is a very simple example case and that I am not
trying to use the above code verbatim, so please refrain from simply
correcting poor programming practices in this example.
Kevin Smith
Kevin.Smith at sas.com
More information about the Python-list
mailing list