By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

strange import phenomenon

P: n/a
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
Sep 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Christoph Zwerschke <ci**@online.de> 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
Sep 21 '05 #2

P: n/a
Thank you, Dieter! Bingo.

When I think about it now, it is very logical: There must be another
mechanism besides sys.path, otherwise modules inside packages would not
find their siblings or subpackages.

But whereever the search path is explained, only sys.path was mentioned,
so I took that at face value. There is a small note in the tutorial, but
it is not very clear and in section 6.4.3 where you don't expect it. The
section 6.1.1 about the module search path does not mention it.

If I want test2.py to find only the modules in the search path, how
should I proceed? One idea that seems to work is setting

__name__ = '__main__'

in test2.py, but I don't know whether that is proper. Any idea?

Thanks again,
Christoph

Sep 22 '05 #3

P: n/a
>One idea that seems to work is setting __name__ = '__main__'

Or, del sys.modules[__name__].
Sep 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.