473,320 Members | 2,094 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,320 software developers and data experts.

Strange import bug

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
5 1800
* 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

"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
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
* 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

"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 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 *
3
by: Rune Strand | last post by:
I'm experiencing strange errors both with pickle and cPickle in the below code: import cPickle as pickle #import pickle from string import ascii_uppercase from string import ascii_lowercase...
0
by: Charles Fineman | last post by:
I've been asked to look over an integration toolkit that has a bunch of schemas to specify message format. There are a couple of strange things I noticed right off the bat and I wanted to get...
3
by: Christoph Zwerschke | last post by:
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...
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...
2
by: unexpected | last post by:
Hey guys, I'm having problems importing a file into my python app. Everytime I try to define the object specified by this file, i.e, test = Test(), It raises an ImportError exception:...
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...
2
by: Spes | last post by:
Hi, I have this simple code: | #!/usr/bin/python | import codecs | import re | from copy import deepcopy | | class MyClass(object): | def __del__(self):
4
by: kj | last post by:
I'm running into a strange seg fault with the module cjson. The strange part is that it does not occur when I run the code under Emacs' Pydb. Here's an example: import sys, cjson d1 =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.