473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strange import phenomenon

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
3 1105
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
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
>One idea that seems to work is setting __name__ = '__main__'

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
1
by: David Scott Williams | last post by:
I have a shell script that exits out with either a 0 or 1...and am experiencing a strange phenomenon... somevalue=os.system("/path/to/script") print "somevalue:"+str(somevalue) If my script...
0
by: Chuck Ritzke | last post by:
I have a page with links that open a second pop-up window to display a chart. I am passing a few data items in hidden fields using a traditional http post (because I can't use a post-back which...
0
by: David Pratt | last post by:
Hi. I am creating a couple of small methods to help me manage time from UTC as standard but I am getting strange results. If I start with a datetime of 2005-12-12 14:30:00 in timezone...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
0
by: Alex Dratva | last post by:
Hi all, I have the following problem with Access 2003 - I try to change the color of some element in a new report (using properties editor). The color of this element stays unchanged and it doesnt...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
3
by: sara | last post by:
Very strange behavior, but I suspect some is A2K and some might be for me to correct. Just trying to see if anyone can help and advise. We have a database that's been running for a few years...
5
by: GD1 | last post by:
Dear developers, I've been working on a Word automator application for some weeks. Now, I formatted my hard drive and reinstalled Visual Studio. When I compiled my project for the first time...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.