Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'package,': 0.03; 'importing': 0.05; 'say,': 0.05; "subject:' ": 0.07; 'tkinter': 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; 'subject:question': 0.10; 'jan': 0.12; '"from': 0.16; '"from"': 0.16; 'editorwindow': 0.16; 'idlelib': 0.16; 'implies': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'two.': 0.16; '(you': 0.16; 'wrote:': 0.18; 'module': 0.19; 'not,': 0.20; '>>>': 0.22; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'convenient': 0.24; 'fairly': 0.24; 'class.': 0.26; 'compare': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'easier': 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; 'there': 0.35; 'throughout': 0.37; 'clear': 0.37; 'sometimes': 0.38; 'confirmed': 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; 'received:org': 0.40; 'most': 0.60; 'name': 0.63; 'real': 0.63; 'refer': 0.63; 'side': 0.67; 'duplication': 0.84; 'etc,': 0.84; 'received:fios.verizon.net': 0.84; 'hand,': 0.93; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Style question: Importing modules from packages - 'from' vs 'as' Date: Wed, 03 Dec 2014 15:13:18 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417637615 news.xs4all.nl 2957 [2001:888:2000:d::a6]:39102 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81871 On 12/3/2014 6:02 AM, 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? I confirmed that they do the same thing for submodules. >>> import os.path as pth >>> from os import path >>> pth >>> path >>> id(pth) 4319096 >>> id(path) 4319096 I and most code I have seen uses "from tkinter import ttk". > 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. "import tkinter.ttk as ttk" makes it clear that ttk is a module rather than, say, a class. That might make things easier for the reader. On the other hand, duplication implies that there might be a real renaming, so having to compare to see that there is not, is extra work. from idlelib import EditorWindow import idlelib.EditorWindow as EditorWindow -- Terry Jan Reedy