strange import phenomenon

Christoph Zwerschke cito at online.de
Tue Sep 20 11:20:37 CEST 2005


Just hitting a strange problem with Python import behavior. It is the 
same on all Python 2.x versions and it is probably correct, but I 
currently don't understand why this happens.

I have created a directory "dir" with the following three module, 
__init__, hello, and test2; and another module test1 in the parent 
directory, like that:


--- test1.py ----------
from dir import test2
------------------------


--- dir/__init__.py ---
print "init"
-----------------------


--- dir/hello.py ------
print "hello world"
-----------------------


--- dir/test2.py ------
import sys
sys.path = []

import hello
-----------------------


The script test2.py removes all entries from the sys.path. So when I run 
test2.py directly, I get an ImportError because the hello module cannot 
be imported. This is as expected.

However, if I run test1, the hello module *is* imported and I get the 
"hello world" message. Why is that??

Probably there is a simple explanation, but currently I simply don't get it.


-- Christoph



More information about the Python-list mailing list