Python Import Statement

In the last lesson, we learned about Python Modules. Now let us learn the different ways to import modules and use their contents.

The Basic import Statement

The simplest way to import a module is:

import module_name

Then we use module_name.something to access its contents.

import math
print(math.pi)
print(math.sqrt(16))

Output

3.141592653589793
4.0

This is clear and safe. We always know where things come from.

Importing with an Alias

Sometimes module names are long. We can give them a shorter alias using as.

import math as m
print(m.pi)
print(m.sqrt(16))

Output

3.141592653589793
4.0

That is shorter and easier to type. Common aliases include import numpy as np and import pandas as pd.

Importing Specific Items from a Module

We can import only what we need using from ... import ....

from math import sqrt, pi
print(sqrt(16))
print(pi)

Output

4.0
3.141592653589793

Now we can use sqrt and pi directly, without the math. prefix.

That is convenient. But be careful. If we import too many names, we might accidentally overwrite something.

Importing with an Alias for Specific Items

We can also give an alias to a specific item.

from math import sqrt as square_root
print(square_root(25))

Output

5.0

That is useful when the name is long or conflicts with something else.

Importing Everything from a Module

We can import all names from a module using *.

from math import *
print(sqrt(16))
print(pi)
print(cos(0))

Output

4.0
3.141592653589793
1.0

This works. But it is not recommended. Because we do not know what names are imported. And it can overwrite our own variables.

Use * only when you are sure it is safe.

Importing Multiple Modules

We can import multiple modules in one line.

import math, random, os
print(math.pi)
print(random.randint(1, 10))
print(os.getcwd())

That is convenient. But it is often clearer to import them on separate lines.

import math
import random
import os

Both work. Choose what is more readable.

Importing a Module from a Subfolder

If our module is inside a folder, we can import it using dot notation.

For example, if we have a folder called utils with a file helpers.py, we can import it like this:

from utils import helpers
helpers.do_something()

We will learn more about this in the packages tutorial.

A Real-Life Example – Using Multiple Imports

import math
from random import randint, choice
from datetime import datetime
print(math.sqrt(25))
print(randint(1, 100))
fruits = ["apple", "banana", "orange"]
print(choice(fruits))
print(datetime.now())

Output (may vary):

5.0
42
banana
2025-01-15 10:30:00.123456

That shows different import styles working together.

Common Mistakes with Import Statements in Python

Mistake 1: Using a name that conflicts with a built-in

from math import *
print = "hello" # overwrites the built-in print

This is bad. We lose the print function.

Fix: Avoid using *. And do not use names that conflict.

Mistake 2: Forgetting the module name after importing

import math
print(sqrt(16))

This gives an error. Because sqrt is not in the global namespace. We need math.sqrt.

Fix:

print(math.sqrt(16))

Mistake 3: Importing a module that does not exist

import mymodule

If mymodule.py is not in the path, this gives an error.

Fix: Make sure the file exists and is in the right folder.

Mistake 4: Using the wrong alias

import math as m
print(math.pi)

This gives an error. Because we aliased math as m. So we must use m.pi.

Fix:

print(m.pi)

Next, we will learn about Creating Python Modules and write reusable code in our own files.