Christoph Zwerschke <[EMAIL PROTECTED]> writes on Tue, 20 Sep 2005 11:20:37 +0200: > 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. > ... > --- 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??
Because Python tries to resolve modules inside a package ("dir" in your example) locally first. This local resolution does not involve "sys.path" but "package.__path__". Only when the local lookup fails, a global lookup (using "sys.path") is tried. In your case, the local lookup succeeds. Dieter -- http://mail.python.org/mailman/listinfo/python-list