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


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

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

Started byWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
First post2014-12-04 23:10 +0100
Last post2014-12-04 23:10 +0100
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2014-12-04 23:10 +0100
SubjectRe: Style question: Importing modules from packages - 'from' vs 'as'
Message-ID<mailman.16590.1417731027.18130.python-list@python.org>
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

[toc] | [standalone]


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


csiph-web