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

Ian Kelly ian.g.kelly at gmail.com
Wed Dec 3 14:52:19 EST 2014


On Dec 3, 2014 4:34 AM, "Chris Angelico" <rosuav at gmail.com> wrote:
>
> On Wed, Dec 3, 2014 at 10:27 PM, Peter Otten <__peter__ at web.de> wrote:
> > Don't repeat yourself, so
> >
> > from os import path
> >
> > always. On the other hand I have never thought about actual renames, e.
g.
> >
> > from os import path as stdpath
> >
> > versus
> >
> > import os.path as stdpath
> >
> > I think I'd use the latter as it looks simpler.
>
> Thanks, Peter and Tim. Keeping DRY is worth doing (the more so as it's
> raining as I type this...), and I won't be renaming in this, so the
> from-import wins - but as Tim says, it's a close race.

To offer a counterpoint, the from import is also less explicit. With
"import os.path as path", path must be a module. With the from import, path
could be either a module or just any attribute of the os module.

My preference when importing modules is to use the fully qualified name --
os.path, not path. If I do a submodule import, I'm probably assigning a
local name anyway, so I still prefer the "import as" over the "from import
as".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141203/c89863cb/attachment.html>


More information about the Python-list mailing list