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


Groups > comp.lang.python > #81923

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

From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: Style question: Importing modules from packages - 'from' vs 'as'
Date 2014-12-04 18:19 +0100
References <CAPTjJmo1-QCgcbsjKNgH3oN-nm5qjB5GrvCiC6Qc9p+S8cNjpg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16569.1417713545.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 12/03/2014 12:02 PM, Chris Angelico wrote:
> When importing a module from a subpackage, it's sometimes convenient
> to refer to it throughout the code with a one-part name rather than
> two. I'm going to use 'os.path' for the examples, but my actual
> use-case is a custom package where the package name is, in the
> application, quite superfluous.
>
> Throughout the code, I want to refer to "path.split()",
> "path.isfile()", etc, without the "os." in front of them. I could do
> either of these:
>
> import os.path as path
> from os import path
>
> Which one would you recommend? Does it depend on context?
>

One argument not yet brought up by anyone else:

if you ever wanted to make the module part of your own package and turn 
the import into a relative one, only the second, but not the first form 
lets you replace the package name with . :

from . import path (while import .path is a SyntaxError, so you'd need a 
slightly more complicated rewrite).

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 18:19 +0100

csiph-web