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

Strange import bug

P: n/a
I have instances where Python 2.3.2 import both does not work or error. It
can be demonstrated under Win2k and Linux by doing the following:

1. Create a subdirectory "abc"
2. In directory "abc" create a blank "__init__.py"
3. In directory "abc" create a file "abc.py" containing "print 'this is
abc'"
4. Interactively run python interpreter
import sys
sys.path.append('./abc')
import abc

It sometimes works for me (in similar situations) if you do:
5. Interactively run python interpreter import sys
sys.path.insert(0,'./abc')
import abc this is abc


Can anyone explain this behaviour please?

Colin Brown
PyNZ

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* Colin Brown [Tue, 25 Nov 2003 11:30:24 +1300]:

My experience with Python is not that large but I'll try to help:
I have instances where Python 2.3.2 import both does not work or error. It
can be demonstrated under Win2k and Linux by doing the following: 1. Create a subdirectory "abc"
2. In directory "abc" create a blank "__init__.py"
3. In directory "abc" create a file "abc.py" containing "print 'this is
abc'"
4. Interactively run python interpreter
import sys
sys.path.append('./abc')
import abc
It sometimes works for me (in similar situations) if you do:
5. Interactively run python interpreter import sys
sys.path.insert(0,'./abc')
import abc

this is abc


The first (default) entry in sys.path is '', which means (I think) the
current directory. So sys.path.append('./abc') appends after this entry;
then, on "import abc", python uses first '' entry, which means it looks
under current directory where it finds "abc" which gets imported *as a
module*.

But, if you place './abc' before '', python will look first *inside*
./abc, considering it a plain directory and not a module. There, under
./abc, it is abc.py, which gets imported by the second "import abc", and
*executed*.
--
Adeodato Simó (a.k.a. thibaut)
EM: asp16 [ykwim] alu.ua.es | IM: my_dato [jabber.org] | PK: DA6AE621

To be nobody but yourself in a world which is doing its best night and
day to make you like everybody else means to fight the hardest battle
any human being can fight and never stop fighting.
-- e.e. cummings

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iEYEARECAAYFAj/CjHkACgkQTxqZjtpq5iHhzQCg39rtrQ8Y8r2p4U9Yaxus5QPK
/gEAn1domZ9yj1PhFU/OnIcTqbu4b19g
=ci2/
-----END PGP SIGNATURE-----

Jul 18 '05 #2

P: n/a

"Adeodato Simó" <as***@alu.ua.es> wrote in message
news:ma*************************************@pytho n.org...
....
The first (default) entry in sys.path is '', which means (I think) the
current directory. So sys.path.append('./abc') appends after this entry;
then, on "import abc", python uses first '' entry, which means it looks
under current directory where it finds "abc" which gets imported *as a
module*.
....

Okay, but why does python not complain when it tries to import the
current directory as a module? I would expect an error, maybe:
"ImportError: xxx is not a valid python module"

If it is considered a valid python module and is executing it then who
knows what it is doing.

There is still also the question of why the prepended path option fails
in some cases under Win2K for me.

Thanks
Colin Brown
PyNZ

Jul 18 '05 #3

P: n/a
Colin Brown wrote:

"Adeodato Simó" <as***@alu.ua.es> wrote in message
news:ma*************************************@pytho n.org...
...
The first (default) entry in sys.path is '', which means (I think) the
current directory.


Not quite true, as I recall. I think it means "the directory from
which the main script was loaded", which is often but not always
the same thing... this is so that other .py files that are in the
same directory as the main script can be found without any other
magic.

(And if you think about it, loading scripts from the current directory
could be a security risk, or at least produce surprising results in
some cases.)

-Peter
Jul 18 '05 #4

P: n/a
* Colin Brown [Tue, 25 Nov 2003 12:41:52 +1300]:
"Adeodato Simó" <as***@alu.ua.es> wrote in message
news:ma*************************************@pytho n.org...
...
The first (default) entry in sys.path is '', which means (I think) the
current directory. So sys.path.append('./abc') appends after this entry;
then, on "import abc", python uses first '' entry, which means it looks
under current directory where it finds "abc" which gets imported *as a
module*.
... Okay, but why does python not complain when it tries to import the
current directory as a module? I would expect an error, maybe:
"ImportError: xxx is not a valid python module"
It doesen't try to import the current directory as a module. If first
looks *inside* the current directory for a module named abc (which can
be a directory named abc with a __init__.py file inside OR an abc.py
file OR an abc.pyc compiled file). So it finds abc, which is a dir but
also a *module* because of the __init__.py file, and imports it.
If it is considered a valid python module and is executing it then who
knows what it is doing.


--
Adeodato Simó (a.k.a. thibaut)
EM: asp16 [ykwim] alu.ua.es | IM: my_dato [jabber.org] | PK: DA6AE621

Truth is the most valuable thing we have, so let's economize it.
-- Mark Twain

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iEYEARECAAYFAj/CmtgACgkQTxqZjtpq5iEEFwCguRepXAenucUXmpNN+SCJJEeb
ru4An2kdXFCYGtzlgiPw0HodqEwtGaY2
=ZmKt
-----END PGP SIGNATURE-----

Jul 18 '05 #5

P: n/a

"Adeodato Simó" <as***@alu.ua.es> wrote in message
news:ma*************************************@pytho n.org...
....
It doesen't try to import the current directory as a module. If first
looks *inside* the current directory for a module named abc (which can
be a directory named abc with a __init__.py file inside OR an abc.py
file OR an abc.pyc compiled file). So it finds abc, which is a dir but
also a *module* because of the __init__.py file, and imports it.
....

Ahh! I see. When "import abc" produces nothing I have to do:
import abc
abc.abc this is abc


Thanks, now I can get on with the original problem I had.

Colin


Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.