4.2) Importing Modules
To use a module, you have to import it (ie load it):
>>> import math
You can then use variables or functions defined within the module. For example, the following prints the constants pi and e and then calculates the square root of 3.2 to the power of 2:
>>> print math.pi >>> print math.e >>> math.sqrt(3.2**2)
Notice that to use functions or variables from a module, you need to write '.' before the specific function/variable. Don’t forget the dot!
If you only want to use one function from a module, you can import only that specific function and you can avoid writing 'math.' before the function name.
>>> from math import sqrt # import the sqrt function from the math module >>> sqrt(3.2**2)
You can do exactly the same for variables.
Finally, you can use an alias for the module, usually to shorten the name:
>>> import math as ma >>> print ma.pi