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 5 1140
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 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 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.
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 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! :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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"...
|
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...
|
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...
|
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__ ==...
|
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::
|
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...
|
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...
|
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...
|
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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |