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


Groups > comp.lang.python > #66452

Re: Absolute imports?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'tree': 0.05; 'python3': 0.07; 'string': 0.09; 'default.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; "wouldn't": 0.14; '"standard': 0.16; '__init__.py': 0.16; 'imports': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'roy': 0.16; 'url:whatsnew': 0.16; 'wrote:': 0.18; 'module': 0.19; 'appears': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'precise': 0.24; 'header:X-Complaints-To:1': 0.27; 'absolute': 0.30; 'relative': 0.30; 'default,': 0.31; 'directory,': 0.31; 'url:python': 0.33; 'but': 0.35; 'version': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'searching': 0.37; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'future': 0.60; 'url:5': 0.61; 'become': 0.64; 'more': 0.64; 'smith': 0.68; 'default': 0.69; '(probably': 0.84; 'does?': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Absolute imports?
Date Sat, 15 Feb 2014 16:46:31 +0100
Organization None
References <roy-34FC23.10062115022014@news.panix.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p57bdacce.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7005.1392479197.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392479197 news.xs4all.nl 2862 [2001:888:2000:d::a6]:60013
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66452

Show key headers only | 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