[Tutor] Question on "import foobar" vs "from foobar import *"

Kent Johnson kent37 at tds.net
Fri Jan 8 21:16:38 CET 2010


On Fri, Jan 8, 2010 at 2:39 PM, Rob Cherry <pythontutor at lxrb.com> wrote:
> Extending on this advice somewhat - is it *ever* correct to "import foobar".
>
> There are countless examples of
>
> import os,sys
>
> etc,etc.  Strictly speaking should we always be using "from" to only
> get what we know we need?

No, the plain import is fine.

import sys
adds just the single name 'sys' to your global namespace, you then
have to access the items in it via sys.version, etc.

from sys import version
imports just the name 'version' into your global namespace.

Kent


More information about the Tutor mailing list