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


Groups > comp.lang.python > #81924

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

Date 2014-12-04 18:36 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: Style question: Importing modules from packages - 'from' vs 'as'
Newsgroups comp.lang.python
Message-ID <mailman.16570.1417714600.18130.python-list@python.org> (permalink)

Show all headers | View raw


----- Original Message -----
> From: "Chris Angelico" <rosuav@gmail.com>
> To: python-list@python.org
> Sent: Wednesday, 3 December, 2014 12:02:17 PM
> Subject: Style question: Importing modules from packages - 'from' vs 'as'
> 
> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
> 

I know you specifically stated you didn't want to do this but

import os

os.path.isfile()

is the best option imo, especially from the reader point of view ("Namespaces are one honking great idea").



-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

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


Thread

Re: Style question: Importing modules from packages - 'from' vs 'as' Jean-Michel Pichavant <jeanmichel@sequans.com> - 2014-12-04 18:36 +0100

csiph-web