Python | Modules | Importing a Module

Modules are Python files that contain a lot of built-in code (variables, functions, etc) that you can use to speed up the writing of your programs. In order to use a module you have to import it. You import a module by using the import keyword followed by the name of the module:

import module_name

You can import more than one module at the time; put commas between the module names.

Let’s take an example:

import math

We import the math module which will output the square root of 25.

Importing a module in Python

Another way to import the math module is as follows:

>>> import math as m
>>> pi = m.pi
>>> print(pi)

Now, you can use m instead of math.

Importing a module in Python

You can also import just a function within a module. For example you can just import sqrt.

from math import sqrt

Leave a Reply