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
using:
import array
array.array
or
from array import array
array
or
from array import array as foobar
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 Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name math
--Nathan Davis
OK Steve, But why do we say "from array import array" and NOT "from math
import math"? Why the difference in syntax?
Gord
"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?