Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66448 > unrolled thread

Absolute imports?

Started byRoy Smith <roy@panix.com>
First post2014-02-15 10:06 -0500
Last post2014-02-15 16:46 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Absolute imports? Roy Smith <roy@panix.com> - 2014-02-15 10:06 -0500
    Re: Absolute imports? Chris Angelico <rosuav@gmail.com> - 2014-02-16 02:26 +1100
    Re: Absolute imports? Peter Otten <__peter__@web.de> - 2014-02-15 16:46 +0100

#66448 — Absolute imports?

FromRoy Smith <roy@panix.com>
Date2014-02-15 10:06 -0500
SubjectAbsolute imports?
Message-ID<roy-34FC23.10062115022014@news.panix.com>
http://docs.python.org/2/whatsnew/2.5.html says:

"Once absolute imports are the default, import string will always find 
the standard libraryšs version."

Experimentally, it appears that modules in site-packages are also found 
by absolute imports.  I wouldn't consider site-packages to be part of 
the "standard library".  Can somebody give me a more precise description 
of what absolute import does?

It also says, "This absolute-import behaviour will become the default in 
a future version (probably Python 2.7)", but it appears that 2.7.6 is 
still doing relative by default.

[toc] | [next] | [standalone]


#66449

FromChris Angelico <rosuav@gmail.com>
Date2014-02-16 02:26 +1100
Message-ID<mailman.7004.1392477977.18130.python-list@python.org>
In reply to#66448
On Sun, Feb 16, 2014 at 2:06 AM, Roy Smith <roy@panix.com> wrote:
> It also says, "This absolute-import behaviour will become the default in
> a future version (probably Python 2.7)", but it appears that 2.7.6 is
> still doing relative by default.
>

Since absolute imports can be controlled with a future directive, you
can check it out via that module:

>>> import __future__
>>> __future__.absolute_import
_Feature((2, 5, 0, 'alpha', 1), (3, 0, 0, 'alpha', 0), 16384)

Looks like it was held over for 3.0 rather than potentially breaking
stuff across 2.6->2.7.

ChrisA

[toc] | [prev] | [next] | [standalone]


#66452

FromPeter Otten <__peter__@web.de>
Date2014-02-15 16:46 +0100
Message-ID<mailman.7005.1392479197.18130.python-list@python.org>
In reply to#66448
Roy Smith wrote:

> http://docs.python.org/2/whatsnew/2.5.html says:
> 
> "Once absolute imports are the default, import string will always find
> the standard library¹s version."
> 
> Experimentally, it appears that modules in site-packages are also found
> by absolute imports.  I wouldn't consider site-packages to be part of
> the "standard library".  Can somebody give me a more precise description
> of what absolute import does?

Basically, if module foo.bar contains an `import baz` with relative imports 
python will look for foo.baz before searching for baz in sys.path; with 
absolute imports `from . import baz` will only look for baz in the current 
package while `import baz` will only search sys.path.

> It also says, "This absolute-import behaviour will become the default in
> a future version (probably Python 2.7)", but it appears that 2.7.6 is
> still doing relative by default.

$ tree
.
├── baz.py
└── foo
    ├── bar.py
    ├── baz.py
    └── __init__.py

1 directory, 4 files
$ cat baz.py
print("import is absolute")
$ cat foo/baz.py 
print("import is relative")
$ cat foo/bar.py 
import baz

$ python -c 'import foo.bar'
import is relative
$ python3 -c 'import foo.bar'
import is absolute

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web