Not understanding absolute_import

Peter Otten __peter__ at web.de
Sun Apr 8 08:37:50 CEST 2007


Nate Finch wrote:

> On Apr 5, 8:33 am, "Nate Finch" <nate.fi... at gmail.com> wrote:
>> I've been trying to use fromabsolute_importand 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.
> 
> 
> No one?  Is this too simple a question, or is it just that no one is
> using this?

The latter, I suppose.

The PEP has the following example:

"""
package/
    __init__.py
    subpackage1/
        __init__.py
        moduleX.py
        moduleY.py
    subpackage2/
        __init__.py
        moduleZ.py
    moduleA.py


Assuming that the current file is either moduleX.py or
subpackage1/__init__.py, following are correct usages of the new syntax:

from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path
"""

I tried that and it turned out that you have to change

from ...package import bar
from ...sys import path

to

from ..package import bar
from ..sys import path

for the code to run without raising an exception. I therefore assume that
the current implementation erroneously mixes the top level and its
immediate child level, and I suggest that you file a bug report.

Peter




More information about the Python-list mailing list