Strange import bug

Adeodato Simó asp16 at alu.ua.es
Mon Nov 24 17:55:53 EST 2003


* 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 204 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20031124/1e3bb65d/attachment.sig>


More information about the Python-list mailing list