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


Groups > comp.lang.python > #81952

Re: Style question: Importing modules from packages - 'from' vs 'as'

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'importing': 0.05; 'explicit': 0.07; 'lines,': 0.07; "subject:' ": 0.07; 'os.path': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:modules': 0.09; 'sure,': 0.09; 'subject:question': 0.10; 'python': 0.11; '2.7': 0.14; 'mostly': 0.14; '"in': 0.16; '__future__': 0.16; 'foo"': 0.16; 'imports': 0.16; 'modules.': 0.16; 'otoh,': 0.16; 'received:80.91.229.3': 0.16; 'received:mediaways.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:pool.mediaways.net': 0.16; 'shifts': 0.16; 'statement.': 0.16; 'top-level': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'typing': 0.19; '>>>': 0.22; 'import': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; '2.x': 0.24; 'certainly': 0.24; 'directory.': 0.24; 'interpret': 0.24; 'earlier': 0.24; 'fine': 0.24; "haven't": 0.24; 'script': 0.25; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'external': 0.29; 'feature': 0.29; 'am,': 0.29; 'dec': 0.30; 'needed.': 0.30; 'relative': 0.30; 'statement': 0.30; "i'm": 0.30; '(which': 0.31; 'code': 0.31; 'usually': 0.31; 'implicit': 0.31; 'ones.': 0.31; 'allows': 0.31; 'checked': 0.32; 'url:python': 0.33; 'fri,': 0.33; 'totally': 0.33; 'actual': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'etc': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; '(e.g.,': 0.36; '2.6': 0.36; 'transition': 0.36; 'words,': 0.36; 'url:org': 0.36; 'project': 0.37; 'question,': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'supporting': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'future': 0.60; 'is.': 0.60; 'url:3': 0.61; "you're": 0.61; 'details': 0.65; 'within': 0.65; 'between': 0.67; 'believe': 0.68; 'statement,': 0.68; 'confusing': 0.84; 'moves': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: Style question: Importing modules from packages - 'from' vs 'as'
Date Thu, 04 Dec 2014 23:10:13 +0100
References <CAPTjJmo1-QCgcbsjKNgH3oN-nm5qjB5GrvCiC6Qc9p+S8cNjpg@mail.gmail.com> <2047880662.4611734.1417714597653.JavaMail.root@sequans.com> <CAPTjJmpgM2n=AdY+8gZ40gqe1S2zfD83Ui-f5SngRYLA3jMtCQ@mail.gmail.com> <m5qhqd$bts$1@ger.gmane.org> <CAPTjJmomvKMBxr0Sq=FtcfDVuUXU9gX6vbzjFdp2VEEk5Qrq1g@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host frbg-5f7310fe.pool.mediaways.net
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
In-Reply-To <CAPTjJmomvKMBxr0Sq=FtcfDVuUXU9gX6vbzjFdp2VEEk5Qrq1g@mail.gmail.com>
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.16590.1417731027.18130.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417731027 news.xs4all.nl 2833 [2001:888:2000:d::a6]:39696
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 6532
X-Received-Body-CRC 1116693581
Xref csiph.com comp.lang.python:81952

Show key headers only | View raw


On 04.12.2014 22:30, Chris Angelico wrote:
> On Fri, Dec 5, 2014 at 7:56 AM, Wolfgang Maier
> <wolfgang.maier@biologie.uni-freiburg.de> wrote:
>> On 04.12.2014 19:05, Chris Angelico wrote:
>>>
>>>
>>> With os.path it definitely is. With the actual code in question, it's
>>> a Python 2.7 project that mostly uses relative imports - inside
>>> package.module1 is "import module2" etc - and I was writing an
>>> external script that calls on one of the modules.
>>
>>
>> What ? I'm usually thinking Python 3 not 2 and I'm never sure which Python
>> 2.x has backported which feature of 3, but I thought implicit relative
>> imports like you seem to describe are not working in 2.7 ?
>
> Hmm, I'm not sure, but certainly it does seem to work that way. Typing
> "import foo" from inside a package will import foo.py from the package
> directory. I haven't dug into the details of _why_, and if ever the
> project shifts to Python 3 (which I would like it to), we might have
> to change some of the import lines, but I'd still like to be able to
> reference "foo.bar" as meaning the "bar" top-level object in foo.py.
>

I checked what the docs say about this and it is totally confusing (at 
least me):

https://docs.python.org/3/howto/pyporting.html#from-future-import-absolute-import 
says:

"
from __future__ import absolute_import

Implicit relative imports (e.g., importing spam.bacon from within 
spam.eggs with the statement import bacon) do not work in Python 3. This 
future statement moves away from that and allows the use of explicit 
relative imports (e.g., from . import bacon).

In Python 2.5 you must use the __future__ statement to get to use 
explicit relative imports and prevent implicit ones. In Python 2.6 
explicit relative imports are available without the statement, but you 
still want the __future__ statement to prevent implicit relative 
imports. In Python 2.7 the __future__ statement is not needed. In other 
words, unless you are only supporting Python 2.7 or a version earlier 
than Python 2.5, use this __future__ statement.
"

which I read as there has been a stepwise transition between 2.5 and 2.7 
so that 2.7 now behaves like Python 3 even without the __future__ statement.
OTOH, I believe you, of course, if you're saying implicit relative 
imports are working just fine in 2.7, but then how to interpret the "In 
Python 2.7 the __future__ statement is not needed." above ?

Wolfgang

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


Thread

Re: Style question: Importing modules from packages - 'from' vs 'as' Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-12-04 23:10 +0100

csiph-web