help with relative imports

Discussion in 'Python' started by John Salerno, Sep 19, 2006.

  1. John Salerno

    John Salerno Guest

    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
    John Salerno, Sep 19, 2006
    #1
    1. Advertising

  2. John Salerno

    Robert Kern Guest

    John Salerno wrote:
    > 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
    Robert Kern, Sep 19, 2006
    #2
    1. Advertising

  3. John Salerno

    John Salerno Guest

    Robert Kern wrote:

    > 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?
    John Salerno, Sep 19, 2006
    #3
  4. John Salerno

    Guest

    John Salerno wrote:
    > Robert Kern wrote:
    >
    > > 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?


    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.
    , Sep 19, 2006
    #4
  5. John Salerno

    Robert Kern Guest

    John Salerno wrote:
    > Robert Kern wrote:
    >
    >> 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?


    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
    Robert Kern, Sep 19, 2006
    #5
  6. John Salerno

    John Salerno Guest

    Robert Kern wrote:

    > 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! :)
    John Salerno, Sep 19, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Smith

    Writing apps without using relative imports

    Randall Smith, Dec 3, 2004, in forum: Python
    Replies:
    0
    Views:
    295
    Randall Smith
    Dec 3, 2004
  2. Chris

    Relative imports

    Chris, Mar 5, 2005, in forum: Python
    Replies:
    4
    Views:
    573
    Kent Johnson
    Mar 5, 2005
  3. Albert
    Replies:
    4
    Views:
    10,869
    Albert
    Jul 10, 2008
  4. zildjohn01
    Replies:
    0
    Views:
    647
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    105
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page