strange import phenomenon

Christoph Zwerschke cito at
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:

--- ----------
from dir import test2

--- dir/ ---
print "init"

--- dir/ ------
print "hello world"

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

import hello

The script removes all entries from the sys.path. So when I run 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