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


Groups > comp.lang.python > #81850

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'package,': 0.03; 'importing': 0.05; "subject:' ": 0.07; 'os.path': 0.09; 'subject:modules': 0.09; 'subject:question': 0.10; '"from"': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'thoughts?': 0.16; 'two.': 0.16; '(you': 0.16; 'module': 0.19; 'code,': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'convenient': 0.24; 'fairly': 0.24; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'another.': 0.31; 'front': 0.32; 'quite': 0.32; 'actual': 0.34; 'subject:from': 0.34; 'could': 0.34; "can't": 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'throughout': 0.37; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'name': 0.63; 'refer': 0.63; 'side': 0.67; 'etc,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=Wuh7WHsfp+eyuhJWhywvSTIP6N87coJ0htW6g5mt8j8=; b=tizXcUwk/TzB4Xwz24xRqBfUVux3VQlcA+D2OdF4W8HoyHePZ5JK9iUPGJiEEPutaw 0rILuFkyqKMAQ77vN6cNClLBT1gimYjHzO/RRbnpGkW0Fy2uiOm1jAQqm7I20jJ9GUVb qj+Tk5NFt3MEyFNUmqv880B/Dx7zTy0gGRxQXoyGy2pnxIK2JBZEAR9ja+bN0TyaFAIn rX75elJWNpDNFXIAJHcaVxDIuh7QC0g+UDTZ/bHoTQ9aH0/Mq8w+2X+Fp3rM/aOy7RdS IpRMBagvOHFfYrWAURBKHshRp++yuvpcC9BLXQu9id5AKqbM7xwJG6gvoJ4AftIW+Khz RQQA==
MIME-Version 1.0
X-Received by 10.42.130.194 with SMTP id w2mr6615845ics.12.1417604537931; Wed, 03 Dec 2014 03:02:17 -0800 (PST)
Date Wed, 3 Dec 2014 22:02:17 +1100
Subject Style question: Importing modules from packages - 'from' vs 'as'
From Chris Angelico <rosuav@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.16525.1417604545.18130.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417604545 news.xs4all.nl 2894 [2001:888:2000:d::a6]:44949
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81850

Show key headers only | View raw


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?

An "as" import works only if it's a module in a package, where the
"from" import can also import other objects (you can't go "import
pprint.pprint as pprint"). I'm fairly sure that's an argument... on
one side or another. :)

Thoughts?

ChrisA

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


Thread

Style question: Importing modules from packages - 'from' vs 'as' Chris Angelico <rosuav@gmail.com> - 2014-12-03 22:02 +1100

csiph-web