Not understanding absolute_import

Nate Finch nate.finch at gmail.com
Thu Apr 5 08:33:30 EDT 2007


I've been trying to use from absolute_import and it's giving me a hell
of a headache.  I can't figure out what it's *supposed* to do, or
maybe rather, it doesn't seem to be doing what I *think* it's supposed
to be doing.

For example (actual example from my code, assume all files have "from
__future__ import absolute_import"):

/project
    /common
         guid.py  (has "class Guid")
         __init__.py  (has "from .guid import Guid")
    /relate
       relatable.py (has "from .common import Guid"  and "class
Relatable(Guid)")
       __init__.py  (has "from .relatable import Relatable")

Now, this all compiles ok and if I change the imports, it does not.
So obviously this is working.  However, I can't figure out *why* it
works.

In relatable.py, shouldn't that need to be "from ..common import
Guid"?  from . should import stuff from the current directory,
from .<foo> should import stuff from module foo in the current
directory. to go up a directory, you should need to use ..   but if I
do that, python complains that I've gone up too many levels.  So, I
don't understand... if the way I have above is correct, what happens
if I put a common.py in the relate directory?  How would you
differentiate between that and the common package?  I don't understand
why .common works from relatable.  According to the docs and according
to what seems to be common sense, it really seems like it should
be ..common.

-Nate




More information about the Python-list mailing list