473,468 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

from __future__ import absolute_import ?


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)
[MSC v.1310 32 bit (Intel)] on win 32
_Ron

Feb 2 '07 #1
7 20525
Ron Adam wrote:
>
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)
[MSC v.1310 32 bit (Intel)] on win 32
If there are two modules 'foo', one at the toplevel and the other inside a
package 'bar',

from __future__ import absolute_import
import foo

will import the toplevel module whereas

import foo

will import bar.foo. A messy demonstration:

$ ls bar
absolute.py foo.py __init__.py relative.py
$ cat bar/absolute.py
from __future__ import absolute_import
import foo
$ cat bar/relative.py
import foo
$ cat foo.py
print "toplevel"
$ cat bar/foo.py
print "in bar"
$ python2.5 -c 'import bar.absolute'
toplevel
$ python2.5 -c 'import bar.relative'
in bar
Another example is here:

http://mail.python.org/pipermail/pyt...ry/422889.html

Peter
Feb 2 '07 #2
Peter Otten wrote:
If there are two modules 'foo', one at the toplevel and the other inside a
package 'bar',

from __future__ import absolute_import
import foo

will import the toplevel module whereas

import foo

will import bar.foo.
.... provided these imports are performed from modules within 'bar'.

Peter
Feb 2 '07 #3
Peter Otten wrote:
Ron Adam wrote:
> 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)
[MSC v.1310 32 bit (Intel)] on win 32

If there are two modules 'foo', one at the toplevel and the other inside a
package 'bar',

from __future__ import absolute_import
import foo

will import the toplevel module whereas

import foo

will import bar.foo. A messy demonstration:

$ ls bar
absolute.py foo.py __init__.py relative.py
$ cat bar/absolute.py
from __future__ import absolute_import
import foo
$ cat bar/relative.py
import foo
$ cat foo.py
print "toplevel"
$ cat bar/foo.py
print "in bar"
$ python2.5 -c 'import bar.absolute'
toplevel
$ python2.5 -c 'import bar.relative'
in bar
Another example is here:

http://mail.python.org/pipermail/pyt...ry/422889.html

Peter
Thanks, that helped, I see why I was having trouble.
work
|
|- foo.py # print "foo not in bar"
|
`- bar
|
|- __init__.py
|
|- foo.py # print "foo in bar"
|
|- absolute.py # from __futer__ import absolute_import
| # import foo
|
`- relative.py # import foo
* Where "work" is in the path.
(1)

C:\work>python -c "import bar.absolute"
foo not in bar

C:\work>python -c "import bar.relative"
foo in bar
(2)

C:\work>python -m "bar.absolute"
foo not in bar

C:\work>python -m "bar.relative"
foo not in bar
(3)

C:\work>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>import bar.absolute
foo not in bar
>>import bar.relative
foo in bar
(4)

C:\work>cd bar

C:\work\bar>python -c "import bar.absolute"
foo in bar

C:\work\bar>python -c "import bar.relative"
foo in bar
(5)

C:\work\bar>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>import bar.absolute
foo in bar
>>import bar.relative
foo in bar
>>>


Case (2) seems like it is a bug.
Why not also have (4), and (5) do the same as cases (1) and (3)?
in cases (4) and (5), that is the result I would expect if I did:

import absolute # with no 'bar.' prefix.
import relative
From what I understand, in 2.6 relative imports will be depreciated, and in 2.7
they will raise an error. (providing plans don't change)

Would that mean the absolute imports in (4) and (5) would either find the 'foo
not in bar' or raise an error?

If so, is there any way to force (warning/error) behavior now?

Cheers,
Ron

Feb 2 '07 #4
Ron Adam wrote:
Peter Otten wrote:
>Ron Adam wrote:
>> 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)
[MSC v.1310 32 bit (Intel)] on win 32

If there are two modules 'foo', one at the toplevel and the other inside
a package 'bar',

from __future__ import absolute_import
import foo

will import the toplevel module whereas

import foo

will import bar.foo. A messy demonstration:

$ ls bar
absolute.py foo.py __init__.py relative.py
$ cat bar/absolute.py
from __future__ import absolute_import
import foo
$ cat bar/relative.py
import foo
$ cat foo.py
print "toplevel"
$ cat bar/foo.py
print "in bar"
$ python2.5 -c 'import bar.absolute'
toplevel
$ python2.5 -c 'import bar.relative'
in bar
Another example is here:

http://mail.python.org/pipermail/pyt...ry/422889.html

Peter

Thanks, that helped, I see why I was having trouble.
work
|
|- foo.py # print "foo not in bar"
|
`- bar
|
|- __init__.py
|
|- foo.py # print "foo in bar"
|
|- absolute.py # from __futer__ import absolute_import
| # import foo
|
`- relative.py # import foo
* Where "work" is in the path.
(1)

C:\work>python -c "import bar.absolute"
foo not in bar

C:\work>python -c "import bar.relative"
foo in bar
(2)

C:\work>python -m "bar.absolute"
foo not in bar

C:\work>python -m "bar.relative"
foo not in bar
(3)

C:\work>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>import bar.absolute
foo not in bar
>>import bar.relative
foo in bar
(4)

C:\work>cd bar
A path below the package level is generally a good means to shoot yourself
in the foot and should be avoided with or without absolute import.
C:\work\bar>python -c "import bar.absolute"
foo in bar

C:\work\bar>python -c "import bar.relative"
foo in bar
(5)

C:\work\bar>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>import bar.absolute
foo in bar
>>import bar.relative
foo in bar
>>>

Case (2) seems like it is a bug.
I think so, too.
Why not also have (4), and (5) do the same as cases (1) and (3)?
The work/bar directory is the current working directory and occurs in the
path before the work directory. When bar.absolute imports foo python is
unaware that work/bar/foo.py is part of the bar package.
in cases (4) and (5), that is the result I would expect if I did:

import absolute # with no 'bar.' prefix.
import relative
From what I understand, in 2.6 relative imports will be depreciated, and
in 2.7
they will raise an error. (providing plans don't change)

Would that mean the absolute imports in (4) and (5) would either find the
'foo not in bar' or raise an error?
No, in 1, 3 -- and 2 if the current behaviour is indeed a bug. This is only
for the relative import which would have to be spelt

from . import foo

in an absolute-import-as-default environment;

import foo

would always be an absolute import.
If so, is there any way to force (warning/error) behavior now?
I don't know.

Peter

Feb 3 '07 #5
Peter Otten wrote:
Ron Adam wrote:
>>
work
|
|- foo.py # print "foo not in bar"
|
`- bar
|
|- __init__.py
|
|- foo.py # print "foo in bar"
|
|- absolute.py # from __futer__ import absolute_import
| # import foo
|
`- relative.py # import foo
* Where "work" is in the path.
(1)

C:\work>python -c "import bar.absolute"
foo not in bar

C:\work>python -c "import bar.relative"
foo in bar
(2)

C:\work>python -m "bar.absolute"
foo not in bar

C:\work>python -m "bar.relative"
foo not in bar
(3)

C:\work>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on win 32
Type "help", "copyright", "credits" or "license" for more information.
> >>import bar.absolute
foo not in bar
> >>import bar.relative
foo in bar
(4)

C:\work>cd bar

A path below the package level is generally a good means to shoot yourself
in the foot and should be avoided with or without absolute import.
Seems so. :-/

>C:\work\bar>python -c "import bar.absolute"
foo in bar

C:\work\bar>python -c "import bar.relative"
foo in bar
(5)

C:\work\bar>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on win 32
Type "help", "copyright", "credits" or "license" for more information.
> >>import bar.absolute
foo in bar
> >>import bar.relative
foo in bar
> >>>

Case (2) seems like it is a bug.

I think so, too.
This one is the reasons I had trouble figuring it out. I was using the -m
command option when I tried to test it.

There is a bug report on absolute/relative imports already. I'm not sure if
this particular item is covered under it or not. Doesn't sound like it as the
bug report address the relative aspects of it.

>Why not also have (4), and (5) do the same as cases (1) and (3)?

The work/bar directory is the current working directory and occurs in the
path before the work directory.
Yes. Unfortunately this is a side effect of using the os's directory structure
to represent a python "package" structure. If a package was represented as a
combined single file. Then the working directory would always be the package
directory.

When bar.absolute imports foo python is
unaware that work/bar/foo.py is part of the bar package.
Umm.... isn't the "bar" stuck on the front of "bar.absolute" a pretty obvious
hint. ;-)

If you run the module directly as a file...

python bar/foo.py
or python foo.py

Then I can see that it doesn't know. But even then, it's possible to find out.
ie... just check for an __init__.py file.

Python has a certain minimalist quality where it tries to do a lot with a
minimum amount of resources, which I generally love. But in this situation that
might not be the best thing. It would not be difficult for python to detect if
a module is in a package, and determine the package location. With the move to
explicit absolute/relative imports, it would make since if python also were a
little smarter in this area.

>in cases (4) and (5), that is the result I would expect if I did:

import absolute # with no 'bar.' prefix.
import relative
From what I understand, in 2.6 relative imports will be depreciated, and
in 2.7
they will raise an error. (providing plans don't change)

Would that mean the absolute imports in (4) and (5) would either find the
'foo not in bar' or raise an error?

No, in 1, 3 -- and 2 if the current behaviour is indeed a bug. This is only
for the relative import which would have to be spelt

from . import foo
Was that a 'yes' for exampels 4 and 5, since 1,2 and 3 are 'no'?

in an absolute-import-as-default environment;

import foo

would always be an absolute import.
But what is the precise meaning of "absolute import" in this un-dotted case?

Currently it is:

"A module or package that is located in sys.path or the current directory".

But maybe a narrower interpretation may be better?:

"A module or package found in sys.path, or the current directory
and is *not* in a package."

If it's in a package then the dotted "absolute" name should be used. Right?
I guess what I'm getting at, is it would be nice if the following were always true.

from __import__ import absolute_import
import thispackage.module
import thispackage.subpackage

# If thispackage is the same name as the current package,
# then do not look on sys.path.
import otherpackage.module
import otherpackage.subpackage

# If otherpackage is a different name from the current package,
# then do not look in this package.
import module
import package

# Module and package are not in a package, even the current one,
# so don't look in any packages, even if the current directory is
# in this (or other) package.
If these were always true, :-) I think it avoid some situations where things
don't work, or don't work like one would expect.

In addition to the above, when executing modules directly from a directory
inside a package, if python were to detect the package and then follow these
same rules. It would avoid even more surprises. While you are editing modules
in a package, you could then run them directly and get the same behavior you get
if you cd'd out of the package and then ran it.

All in all, what I'm suggesting is that the concept of a package (type) be much
stronger than that of a search path or current directory. And that this would
add a fair amount of reliability to the language.

IMHO, of course. :-)

Cheers,
Ron
>If so, is there any way to force (warning/error) behavior now?

I don't know.

Peter
Feb 3 '07 #6
Ron Adam wrote:
Peter Otten wrote:
>Ron Adam wrote:
>>>
work
|
|- foo.py # print "foo not in bar"
|
`- bar
|
|- __init__.py
|
|- foo.py # print "foo in bar"
|
|- absolute.py # from __futer__ import absolute_import
| # import foo
|
`- relative.py # import foo
* Where "work" is in the path.
(1)

C:\work>python -c "import bar.absolute"
foo not in bar

C:\work>python -c "import bar.relative"
foo in bar
(2)

C:\work>python -m "bar.absolute"
foo not in bar

C:\work>python -m "bar.relative"
foo not in bar
(3)

C:\work>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>import bar.absolute
foo not in bar
>>import bar.relative
foo in bar
(4)

C:\work>cd bar

A path below the package level is generally a good means to shoot
yourself in the foot and should be avoided with or without absolute
import.

Seems so. :-/

>>C:\work\bar>python -c "import bar.absolute"
foo in bar

C:\work\bar>python -c "import bar.relative"
foo in bar
(5)

C:\work\bar>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>import bar.absolute
foo in bar
>>import bar.relative
foo in bar
>>>

Case (2) seems like it is a bug.

I think so, too.

This one is the reasons I had trouble figuring it out. I was using the -m
command option when I tried to test it.

There is a bug report on absolute/relative imports already. I'm not sure
if
this particular item is covered under it or not. Doesn't sound like it as
the bug report address the relative aspects of it.

>>Why not also have (4), and (5) do the same as cases (1) and (3)?

The work/bar directory is the current working directory and occurs in the
path before the work directory.

Yes. Unfortunately this is a side effect of using the os's directory
structure
to represent a python "package" structure. If a package was represented
as a
combined single file. Then the working directory would always be the
package directory.

When bar.absolute imports foo python is
unaware that work/bar/foo.py is part of the bar package.

Umm.... isn't the "bar" stuck on the front of "bar.absolute" a pretty
obvious
hint. ;-)

If you run the module directly as a file...

python bar/foo.py
or python foo.py

Then I can see that it doesn't know. But even then, it's possible to find
out.
ie... just check for an __init__.py file.

Python has a certain minimalist quality where it tries to do a lot with a
minimum amount of resources, which I generally love. But in this
situation that
might not be the best thing. It would not be difficult for python to
detect if
a module is in a package, and determine the package location. With the
move to explicit absolute/relative imports, it would make since if python
also were a little smarter in this area.
I have not used the new import behaviour seriously, but -- I think I like
it :-)
>>in cases (4) and (5), that is the result I would expect if I did:

import absolute # with no 'bar.' prefix.
import relative
From what I understand, in 2.6 relative imports will be depreciated,
and in 2.7
they will raise an error. (providing plans don't change)

Would that mean the absolute imports in (4) and (5) would either find
the 'foo not in bar' or raise an error?

No, in 1, 3 -- and 2 if the current behaviour is indeed a bug. This is
only for the relative import which would have to be spelt

from . import foo

Was that a 'yes' for exampels 4 and 5, since 1,2 and 3 are 'no'?
(4) and (5) are misconfigurations, IMHO.
>in an absolute-import-as-default environment;

import foo

would always be an absolute import.

But what is the precise meaning of "absolute import" in this un-dotted
case?

Currently it is:

"A module or package that is located in sys.path or the current
directory".

But maybe a narrower interpretation may be better?:

"A module or package found in sys.path, or the current directory
and is *not* in a package."
You'd have to add a not-in-package test to every import - I don't think it's
worth the effort.
If it's in a package then the dotted "absolute" name should be used.
Right?
Either that or the full path. The dotted path makes it easy to move the
module between packages.
I guess what I'm getting at, is it would be nice if the following were
always true.

from __import__ import absolute_import
import thispackage.module
import thispackage.subpackage

# If thispackage is the same name as the current package,
# then do not look on sys.path.
import otherpackage.module
import otherpackage.subpackage

# If otherpackage is a different name from the current package,
# then do not look in this package.
import module
import package

# Module and package are not in a package, even the current one,
# so don't look in any packages, even if the current directory is
# in this (or other) package.
If these were always true, :-) I think it avoid some situations where
things don't work, or don't work like one would expect.

In addition to the above, when executing modules directly from a directory
inside a package, if python were to detect the package and then follow
these
same rules. It would avoid even more surprises. While you are editing
modules in a package, you could then run them directly and get the same
behavior you get if you cd'd out of the package and then ran it.

All in all, what I'm suggesting is that the concept of a package (type) be
much
stronger than that of a search path or current directory. And that this
would add a fair amount of reliability to the language.
I think if you go that way, ultimately you will need some kind of package
registry. I expect that the new import behaviour will get you 99 percent
there with one percent of the hassle. But we will see...

Peter
Feb 9 '07 #7
Peter Otten wrote:
Ron Adam wrote:
>Peter Otten wrote:
>>Ron Adam wrote:

work
|
|- foo.py # print "foo not in bar"
|
`- bar
|
|- __init__.py
|
|- foo.py # print "foo in bar"
|
|- absolute.py # from __futer__ import absolute_import
| # import foo
|
`- relative.py # import foo
>>>(4)
>>>C:\work\bar>python -c "import bar.absolute"
foo in bar
>>>(5)
>>> >>import bar.absolute
foo in bar
(4) and (5) are misconfigurations, IMHO.
But it's a very common configuration. So it will most likely cause problems for
someone.

From what I understand these will probably do what I want in python 2.6, which
is either import the foo not in bar, or give an error if foo not in bar doesn't
exist instead of importing foo in bar.

>>in an absolute-import-as-default environment;

import foo

would always be an absolute import.
But what is the precise meaning of "absolute import" in this un-dotted
case?

Currently it is:

"A module or package that is located in sys.path or the current
directory".

But maybe a narrower interpretation may be better?:

"A module or package found in sys.path, or the current directory
and is *not* in a package."

You'd have to add a not-in-package test to every import - I don't think it's
worth the effort.
No, you only need to test the (first) module you explicitly run is in a package.
For any imports after that, the absolute import code can exclude any of the
package directories for un-dotted top level absolute imports. It may be a
performance net gain because there is less disk searching.

>All in all, what I'm suggesting is that the concept of a package (type) be
much
stronger than that of a search path or current directory. And that this
would add a fair amount of reliability to the language.

I think if you go that way, ultimately you will need some kind of package
registry. I expect that the new import behaviour will get you 99 percent
there with one percent of the hassle. But we will see...
It won't need a registry.

Check the python-ideas list for further discussion on this.

Cheers,
Ron

Feb 9 '07 #8

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

Similar topics

3
by: Logan | last post by:
Is there a list with all 'from __future__ import ...' statements (which lists all the statements, in which version of Python the feature was introduced and in which version of Python it will become...
8
by: Jacek Generowicz | last post by:
I have some code, which makes copious use of the @decorator syntax which was introduced in Python2.4. Now I find myself in a situation where I have to run the code under Python 2.3. However, I...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
5
by: mark_galeck_spam_magnet | last post by:
Hi, why does complain name 'compileFile' not defined. But works. Why? (I did read the tutorial, it seems to say "import module" should work. Thank you, Mark
2
by: mithrond | last post by:
i can't use "from __future__ import ..." statement import two statesments in the same time. for the simple code: from __future__ import with_statement, division with file('url.txt','r') as f:...
7
by: samslists | last post by:
Am I the only one that thinks this would be useful? :) I'd really like to be able to use python 3.0's print statement in 2.x. Is this at least being considered as an option for 2.6? It seems...
1
by: Malcolm Greene | last post by:
Is there any consensus on what "from __future__ import" options developers should be using in their Python 2.5.2 applications? Is there a consolidated list of "from __future__ import" options to...
0
by: Tim Golden | last post by:
Malcolm Greene wrote: Well, that bit's easy: import __future__ print __future__.all_feature_names TJG
3
by: notnorwegian | last post by:
import Tkinter from Tkinter import * i have a program where if i comment out either of those import- statements i get an error. i thought they meant the same thing and from was supposed to be...
1
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...
0
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...
0
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...
0
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,...
0
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,...
1
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.