Not understanding absolute_import

N

Nate Finch

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
 
N

Nate Finch

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?

-Nate
 
P

Peter Otten

Nate said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top