davisn90210 at davisn90210 at
Wed Nov 14 03:51:09 CET 2007

Modules contain objects.  When you want to import a specific set of
objects contained in a module into the local namespace, you use:
  from <module> import <names to import>
For example:
  from math import sqrt
  from math import sin, cos

If you want to import everything from a module, use:
  from <module> import *
For example:
  from math import *

If you want to import a module as an entity itself, use:
  import <module>
For example:
  import math #Use, for example, math.sqrt to call the sqrt function

You can also use import ... as ... to "rename" an imported object:
  import math as m #Now use m.sqrt instead of math.sqrt
  from math import sqrt as square_root #sqrt is now bound to
square_root in the local module

In the array module, there is an object named array.  You could access
  import array
  from array import array
  from array import array as foobar
or ...
The way you choose to actually do this is up to you.  Most people just
decide what they like/makes the most sense.

In the math module, there is no object called math.  That is why we do
not use
  >>> from math import math
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  ImportError: cannot import name math

--Nathan Davis

On Nov 13, 7:25 pm, "Gordon C" <gc... at> wrote:
> OK Steve, But why do we say "from array import array" and NOT  "from math
> import math"? Why the difference in syntax?
> Gord
> "Steven D'Aprano" <st... at> wrote in message
> news:13jk6af1vs5g89 at
> > On Tue, 13 Nov 2007 11:26:28 -0500, Gordon C wrote:
> >> OK, thanks to all. The key statement is "from array import array" which
> >> is not exactly intuitive!
> > "The only intuitive interface is the nipple. After that, it's all
> > learned."  --  Bruce Ediger on user interfaces.
> > Once you've been using Python for a while, using import becomes as
> > intuitive as a spoon. The only tricky part is knowing *which* module to
> > import. But, honestly, are you surprised to learn that the array type is
> > held in in the array module?
> > --
> > Steven.

More information about the Python-list mailing list