470,849 Members | 1,121 Online

# 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 20117
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 discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 3 posts views Thread by Logan | last post: by 8 posts views Thread by Jacek Generowicz | last post: by 3 posts views Thread by Mudcat | last post: by 5 posts views Thread by mark_galeck_spam_magnet | last post: by 2 posts views Thread by mithrond | last post: by 7 posts views Thread by samslists | last post: by 1 post views Thread by Malcolm Greene | last post: by reply views Thread by Tim Golden | last post: by 3 posts views Thread by notnorwegian | last post: by reply views Thread by ryjfgjl | last post: by reply views Thread by AlexandraMT | last post: by reply views Thread by prepawan | last post: by 1 post views Thread by DANILIN | last post: by reply views Thread by tracyyun | last post: by reply views Thread by jackhack | last post: by reply views Thread by shivajikobardan | last post: by reply views Thread by gglobus | last post: by reply views Thread by zachatmarco | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.