from __future__ import absolute_import issue

LittleGrasshopper seattlehanks at
Sat May 23 11:32:24 EDT 2009

On May 22, 12:42 am, "Gabriel Genellina" <gagsl-... at>
> En Wed, 20 May 2009 20:18:02 -0300, LittleGrasshopper  
> <seattleha... at> escribió:
> > New to the group, this is my first post...
> > It appears that either absolute imports (or my brain) aren't working.
> > Given a module which is in the same directory as
> > #File
> > from __future__ import absolute_import
> > import string
> > print string # Module imported is in current directory, not
> > standard library module
> absolute_import helps with imports inside a package (those are "relative"  
> imports, relative to the package). If a module is located in a directory  
> listed in sys.path, an absolute import will find it. I don't think there  
> is a way to avoid shadowing a standard module (other than ensuring the  
> standard directories come first in sys.path).
> --
> Gabriel Genellina

You are right. If you have a directory in your PYTHONPATH before the
standard library directories that has a string module, for example,
absolute_import will not help you. I was getting confused by the fact
that when I print sys.path from the python shell, the first entry is
an empty string.
This empty string must denote the current directory though, because:

['', '/usr/lib/', '/usr/lib/python2.5', '/usr/lib/
python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/
python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/
usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/
Numeric', '/usr/lib/python2.5/site-packages/PIL', '/usr/lib/python2.5/
site-packages/gst-0.10', '/var/lib/python-support/python2.5', '/usr/
lib/python2.5/site-packages/gtk-2.0', '/var/lib/python-support/

When I have this sys.path, doing an "import string" from a module
where I have absolute imports enabled will still import the string
module in the package (which also means it is in the same directory in
this case.) So I think my is being imported not because it
is in the same package, but because the home directory is searched
first. The empty string doesn't make much sense though (at least in a
Unix system, you would imagine it would be something like './') but I
guess that synce Python is platform independent, the empty string is
taken to denote the current directory.

Thanks for your help.

More information about the Python-list mailing list