473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Not understanding absolute_import

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 .<fooshould 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

Apr 5 '07 #1
2 1388
On Apr 5, 8:33 am, "Nate Finch" <nate.fi...@gmail.comwrote:
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
Apr 8 '07 #2
Nate Finch wrote:
On Apr 5, 8:33 am, "Nate Finch" <nate.fi...@gmail.comwrote:
>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?
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

Apr 8 '07 #3

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

Similar topics

2
by: Mike | last post by:
Hey guys, need some help understanding some things that maybe someone can explain or clarify it a little better then a text book. Here is my understanding so far: Class - basically a shell for...
7
by: laura | last post by:
I'm trying to understand fully Server.MapPath. I am writing an intranet for a small company and want to be able to put some files accessible to all, hyperlinked from the intranet and therefore,...
19
by: Pugi! | last post by:
Currently I am studying PHP. I can understand the following : $a = 10; $b = $a++; print("$a, $b"); will print 11, 10 on the screen. because when $b = $a++ first thing that happens is $b = $a...
26
by: Bail | last post by:
I will have a exam on the oncoming friday, my professor told us that it will base upon this program. i am having troubles understanding this program, for example what if i want to add all the...
11
by: Leon | last post by:
Are dataset automatically stored in memory? Does the dispose() method automatically dispose of the dataset in the code below? Do I have to dispose a dataset from memory or does the dataset...
18
by: Simon | last post by:
Hi, I understand what one the differences between std::vector, std::deque and std::list is, the std::vector can have data inserted/deleted at the end. The std::deque can have data...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
8
by: boki_pfc | last post by:
Hi Everybody, I am looking for an advice on following: I have that "pleasure" of reading C++ codes that have been written by person(s) that have not attended the same C++ classes that I did or...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.