Strange import bug

C

Colin Brown

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
It sometimes works for me (in similar situations) if you do:
5. Interactively run python interpreter
Can anyone explain this behaviour please?

Colin Brown
PyNZ
 
A

Adeodato =?iso-8859-1?Q?Sim=F3?=

* 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
It sometimes works for me (in similar situations) if you do:
5. Interactively run python interpreter
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-----
 
C

Colin Brown

....
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
 
P

Peter Hansen

Colin said:
...
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
 
A

Adeodato =?iso-8859-1?Q?Sim=F3?=

* Colin Brown [Tue, 25 Nov 2003 12:41:52 +1300]:
...
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-----
 
C

Colin Brown

....
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:

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

Colin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top