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


Groups > comp.lang.python > #66452

Re: Absolute imports?

From Peter Otten <__peter__@web.de>
Subject Re: Absolute imports?
Date 2014-02-15 16:46 +0100
Organization None
References <roy-34FC23.10062115022014@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.7005.1392479197.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web