473,659 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with relative imports

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
Sep 19 '06 #1
5 1148
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

Sep 19 '06 #2
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?
Sep 19 '06 #3

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 '06 #4
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

Sep 19 '06 #5
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! :)
Sep 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1580
by: Rutger Claes | last post by:
In the directory /var/www/dynamo/xsl is the basic xsl file for xhtml documents /var/www/dynamo/xsl/xhtml.xsl. In this file there is an import statement: <xsl:import href="xhtml/basic.xhtml.xsl" />. This import statement should load /var/www/dynamo/xsl/xhtml/basic.xhtml.xsl as far as I know. The current document is /var/www/dynamo/xml/index.xml (.xml documents are handled by the PHP parser on my server). This is the error message I get:...
0
1354
by: Randall Smith | last post by:
I've noticed the push by Guido and others to use absolute imports instead of relative imports. I've always enjoyed the ease of relative imports, but am starting to understand that "explicit is better than implicitly" as the Python philosophy goes. I'm trying to develop a strategy for writing packages using only absolute imports and would really like to know what others have learned. Randall
0
1453
by: Anders J. Munch | last post by:
Now 2.5 is out, and we have a syntax for explicit relative imports (PEP 328, http://www.python.org/dev/peps/pep-0328/, in case anyone wasn't paying attention). The long-term plan is that the classical import syntax becomes absolute import only, so that all imports are explicitly one or the other. You can only use relative import from within packages. Trying to import a top-level module relatively from within same directory, produces...
1
5685
by: Alexey Borzenkov | last post by:
After reading PEP-0328 I wanted to give relative imports a try: # somepkg/__init__.py <empty> # somepkg/test1.py from __future__ import absolute_import from . import test2 if __name__ == "__main__":
12
2730
by: Alan Isaac | last post by:
Are relative imports broken in 2.5? Directory ``temp`` contains:: __init__.py test1.py test2.py File contents: __init__.py and test2.py are empty test1.py contains a single line::
9
2165
by: rbygscrsepda | last post by:
Hi, I'm a newbie at Python. :) Right now it's not letting me import * from any relative package name--i.e., a name that starts with a dot. For instance, none of the following work: from . import * from .sibiling import * from .. import * from ..parent_sibling import * ....and so on. The same error occurs:
0
1390
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with them. Opening the tutorial I found following notice ( 6.4.2 ): "Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules
4
1828
by: DG | last post by:
Alright, I have searched and searched and read many conversations on the topic of relative and absolute imports and am still not getting the whole thing through my skull. Highlights of what I've read: http://mail.python.org/pipermail/python-list/2007-January/422973.html http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1e8dc93471a7079/8751c82cfe1ca3f2?lnk=gst&q=absolute+import#8751c82cfe1ca3f2...
0
1491
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for me. Yes.
0
8339
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.