Python 2.7: Importing Modules

Modules make your code reusable and sharable amount different files. Module is a file that contains definitions - including variables and functions - that you can use once its imported.

Some modules are built-in and will expose you to their functions once imported. Example below shows how sqrt function becomes available once the math module is imported:

# importing math module import math print math.sqrt(25)

importing specific functions / variables frmo math module

from math import sqrt print sqrt(25)

importing all the definitions form match module and unwrapping math.

from math import * print sqrt(25)

Of course, universal importing is not safe. You may fill your code with unnecessary variables and definitions which may conflict by using the same definition names between the modules.

Displays what is inside the module (all definitions in form of array of strings):

import math everything = dir(math) print everything