Import Statement in Python
Learn how to import modules, functions, classes and variables into a Python program.
What is an Import Statement?
Python provides the import statement
to use code from another module.
Instead of writing the same code again, we can create reusable code in a module and import it whenever we need it.
import statement is used to bring
a module or its functionality into our Python program.
Why Do We Use Import?
- Reuse existing code.
- Avoid duplicate code.
- Use Python's built-in modules.
- Use our own user-defined modules.
- Use functions and classes created in other files.
Import Statement Diagram
The basic idea of importing a module can be understood like this:
Python Program
main.py
import
Requests reusable code from another module.
Module
math
Function
sqrt(), add()
Class
User-defined class
Reuse the Code
The imported functionality can now be used in the current program.
1. Importing a Complete Module
The simplest way to import a module is:
import math
print(math.sqrt(25))
print(math.pi)
Output:
5.0
3.141592653589793
Here, math is imported and its
functions or variables are accessed using:
module_name.item_name
Example:
math.sqrt()
2. Importing a User-Defined Module
Suppose we create a file named
calculator.py.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
Now create another file:
main.py
import calculator
print(calculator.add(10, 20))
print(calculator.subtract(20, 5))
Output:
30
15
We can reuse the functions from
calculator.py without writing them again.
3. Using from ... import
We can import only a specific function, class or variable instead of importing the complete module.
from math import sqrt
print(sqrt(36))
Output:
6.0
Now we can use sqrt() directly without
writing math.sqrt().
Importing Multiple Items
We can import more than one item from a module.
from math import sqrt, pi
print(sqrt(49))
print(pi)
Output:
7.0
3.141592653589793
Both sqrt and pi are imported
from the math module.
4. Using an Alias with as
We can give a shorter name, called an alias, to a module.
import math as m
print(m.sqrt(64))
print(m.pi)
Output:
8.0
3.141592653589793
Here, m is an alias for the
math module.
Importing an Item with an Alias
We can also give an alias to a specific imported item.
from math import sqrt as square_root
print(square_root(81))
Output:
9.0
Here, sqrt is imported with the name
square_root.
5. Importing Everything
Python also supports importing all public names
from a module using the * symbol.
from math import *
print(sqrt(25))
print(pi)
This allows us to use imported names directly.
from module import * is generally
avoided in larger applications because it can make
it difficult to understand where names came from
and can cause naming conflicts.
6. Importing a Class
We can import a class from another module.
Suppose student.py contains:
class Student:
def __init__(self, name):
self.name = name
def display(self):
print("Name:", self.name)
Now in main.py:
from student import Student
student1 = Student("Rahul")
student1.display()
Output:
Name: Rahul
7. Importing a Variable
Variables can also be imported from another module.
Suppose config.py contains:
company_name = "CIIT Training Institute 🔓🌍...!"
city = "Pune"
Now:
from config import company_name, city
print(company_name)
print(city)
Output:
CIIT Training Institute 🔓🌍...!
Pune
import vs from ... import
| import | from ... import |
|---|---|
| Imports the module. | Imports selected items. |
Example:
import math
|
Example:
from math import sqrt
|
Use:
math.sqrt()
|
Use:
sqrt()
|
Common Modules We Can Import
math
Mathematical functions and constants.
random
Generate random values and selections.
datetime
Work with dates and times.
os
Work with operating-system related functionality.
json
Work with JSON data.
re
Work with regular expressions.
Common Mistakes
- Writing the wrong module name.
- Forgetting the correct import syntax.
- Importing a function but using the module prefix.
- Creating naming conflicts by importing many names.
-
Using
import *unnecessarily.
Example 🤓👀
Suppose we create a module named
calculator.py:
def add(a, b):
return a + b
def multiply(a, b):
return a * b
Then in main.py:
from calculator import add, multiply
result1 = add(10, 20)
result2 = multiply(5, 4)
print("Addition:", result1)
print("Multiplication:", result2)
Output:
Addition: 30
Multiplication: 20
Summary :
The import statement is used to reuse code from
another Python module. We can import a complete module
using import, import selected items using
from ... import, create shorter names using
as, and import user-defined modules,
functions, classes and variables. Using imports helps
us organize code and avoid duplication.