help with relative imports

J

John Salerno

I'm reading the "What's New" section of the 2.5 docs, and I'm a little
confused by the last section of "Absolute and Relative Imports":

-----------------------------------------------
For example, code in the A.B.C module can do:

from . import D # Imports A.B.D
from .. import E # Imports A.E
from ..F import G # Imports A.F.G
-----------------------------------------------

Can someone explain this? It seems like information is missing. How do
you know where D, E, F and G are to figure this out? If all you are
given is A.B.C, and then someone gives you the above three examples,
what steps do you take to figure out what gets imported from where?

Thanks,
John
 
R

Robert Kern

John said:
I'm reading the "What's New" section of the 2.5 docs, and I'm a little
confused by the last section of "Absolute and Relative Imports":

-----------------------------------------------
For example, code in the A.B.C module can do:

from . import D # Imports A.B.D
from .. import E # Imports A.E
from ..F import G # Imports A.F.G
-----------------------------------------------

Can someone explain this? It seems like information is missing. How do
you know where D, E, F and G are to figure this out? If all you are
given is A.B.C, and then someone gives you the above three examples,
what steps do you take to figure out what gets imported from where?

What is ambiguous about A.B.D, A.E, and A.F.G? But if you like:

A/
B/
C.py
D.py
E.py
F/
G.py

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
J

John Salerno

Robert said:
What is ambiguous about A.B.D, A.E, and A.F.G? But if you like:

I guess maybe I was looking at it backwards. From the way it was worded,
I thought the only information we had to use was the structure A.B.C,
and then given a statement like:

from . import D

we just had to figure out for ourselves that this results in A.B.D,
instead of, for example, A.C.D, or any other possibility.

But I'm still a little confused about the use of the single or double
period. In this case:

from . import D # Imports A.B.D
from .. import E # Imports A.E

why do you need a single period in the first example, and a double in
the second, if they both are importing from A? If E is directly under A,
couldn't you just use a single period? And since D is nested twice
beneath A (i.e., in A, then in B), wouldn't you need two periods there
instead?
 
R

richard.charts

John said:
I guess maybe I was looking at it backwards. From the way it was worded,
I thought the only information we had to use was the structure A.B.C,
and then given a statement like:

from . import D

we just had to figure out for ourselves that this results in A.B.D,
instead of, for example, A.C.D, or any other possibility.

But I'm still a little confused about the use of the single or double
period. In this case:

from . import D # Imports A.B.D
from .. import E # Imports A.E

why do you need a single period in the first example, and a double in
the second, if they both are importing from A? If E is directly under A,
couldn't you just use a single period? And since D is nested twice
beneath A (i.e., in A, then in B), wouldn't you need two periods there
instead?

I was staring at this too for a bit.
You just have to remember basic directory relative paths:
.. -> Current Directory
... -> One level up from current.

At least I'm assuming this is right based on the test and the example
above.
 
R

Robert Kern

John said:
I guess maybe I was looking at it backwards. From the way it was worded,
I thought the only information we had to use was the structure A.B.C,
and then given a statement like:

from . import D

we just had to figure out for ourselves that this results in A.B.D,
instead of, for example, A.C.D, or any other possibility.

But I'm still a little confused about the use of the single or double
period. In this case:

from . import D # Imports A.B.D
from .. import E # Imports A.E

why do you need a single period in the first example, and a double in
the second, if they both are importing from A? If E is directly under A,
couldn't you just use a single period? And since D is nested twice
beneath A (i.e., in A, then in B), wouldn't you need two periods there
instead?

Remember that this is code in the A.B.C module. The first form looks for modules
in the A.B package, that is, next to A.B.C . The second looks for modules in the
A package, next to A.B .

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
J

John Salerno

Robert said:
Remember that this is code in the A.B.C module.

Oh! That clears it all up! I wasn't realizing that the import statements
were being executed from within the C module! Thanks! :)
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top