- Learning Objectives
- Prerequisites
- Introduction
- What is an Import Statement?
- Why is the Import Statement Required?
- Different Ways to Import Modules
- Importing Multiple Modules
- Module Search Process
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand the purpose of the import statement.
- Import built-in modules.
- Import user-defined modules.
- Import specific members from a module.
- Import multiple modules.
- Understand Python's module search process.
- Follow best practices for importing modules.
Before learning this chapter, you should know:
- Modules
- Custom Modules
- Functions
- Classes
Python provides thousands of reusable functions and classes through modules.
To use the contents of a module,
the module must first be imported into the current program.
The import statement tells Python to load a module so that its functions, classes, and variables can be used.
An import statement is used to load a module into the current Python program.
After importing,
the module's contents become available for use.
The import statement allows one Python program to use code written in another module.
Without importing a module,
its functions, classes, and variables cannot be accessed.
Importing modules allows:
- Code reuse
- Better organization
- Reduced duplication
- Easier maintenance
- Faster development
Python provides several ways to import modules.
Syntax
import mathUsage
import math
print(math.sqrt(25))Every member is accessed using the module name.
Syntax
from math import sqrtUsage
from math import sqrt
print(sqrt(25))Only the required member is imported.
Syntax
from math import sqrt, factorialExample
from math import sqrt, factorial
print(sqrt(36))
print(factorial(5))Syntax
from math import *Example
from math import *
print(sqrt(49))Note: Wildcard imports are generally discouraged because they can make code harder to read and may cause name conflicts.
Syntax
import math
import randomor
import math, randomExample
import calculatorPython searches for
calculator.py
and imports it.
When Python encounters an import statement,
it searches in the following order:
- Current project directory
- Directories listed in
PYTHONPATH - Python standard library
- Installed third-party packages
If the module is not found,
Python raises
ModuleNotFoundErrorProgram Starts
↓
Import Statement
↓
Python Searches Module
↓
Module Found?
↓
Yes
↓
Load Module
↓
Execute Module Once
↓
Module Object Created
↓
Program Continues
-------------------------
No
↓
ModuleNotFoundError
- Encourages code reuse.
- Improves readability.
- Makes applications modular.
- Simplifies maintenance.
- Reduces duplicate code.
- Import only what is needed.
- Place import statements at the top of the file.
- Prefer explicit imports over wildcard imports.
- Use meaningful module names.
- Organize imports logically.
Using a module without importing it.
Using wildcard imports unnecessarily.
Misspelling module names.
Creating custom modules with names that conflict with built-in modules.
Example
random.py
math.py
Avoid such names.
The import statement loads a module so that its functions, classes, variables, and other members can be used in the current program.
import modulefrom module import memberfrom module import member1, member2from module import *
Using explicit imports such as
import mathor
from math import sqrtbecause they improve readability and avoid namespace conflicts.
Python raises
ModuleNotFoundErrorBecause they can introduce namespace conflicts and make code more difficult to understand and maintain.
import modulefrom module import memberfrom module import member1, member2from module import *(use sparingly)- Import statements should usually be placed at the top of the file.
- Python searches predefined locations to find modules.
In this chapter, we learned:
- Import Statement
- Different Import Methods
- Importing Built-in Modules
- Importing User-Defined Modules
- Module Search Process
- Best Practices
- Common Mistakes
- Interview Questions
- Quick Revision
Write a Python program to import the math module and calculate the square root of a number.
import math
number = 81
result = math.sqrt(number)
print("Square Root :", result)Square Root : 9.0
The entire math module is imported.
The required function is accessed using
math.sqrt()from math import sqrt
print(sqrt(100))10.0
Only the sqrt() function is imported.
The module name is not required while calling the function.
from math import sqrt, factorial
print(sqrt(64))
print(factorial(5))8.0
120
Multiple functions can be imported from the same module.
from math import *
print(sqrt(49))
print(pow(2, 5))7.0
32.0
The wildcard (*) imports all public members from the module.
Although supported,
this approach is generally not recommended because it may cause naming conflicts.
import math
import random
print(math.factorial(6))
print(random.randint(1, 10))720
4
Multiple modules can be imported into the same Python program.
def add(a, b):
return a + bimport calculator
print(calculator.add(20, 30))50
Python imports the user-defined module
calculator.py
and makes its functions available.
def add(a, b):
return a + b
def subtract(a, b):
return a - bfrom calculator import add, subtract
print(add(15, 5))
print(subtract(15, 5))20
10
Only the required functions are imported.
This keeps the namespace cleaner.
import samplemoduleModuleNotFoundError:
No module named 'samplemodule'
Python raises
ModuleNotFoundErrorwhen it cannot locate the specified module.
import datetime
current = datetime.datetime.now()
print(current)2026-07-01 14:45:10.123456
The datetime module provides classes for working with dates and times.
import os
print(os.getcwd())
print(os.name)C:\Users\Student\Python
nt
The os module provides operating system related functionality.
getcwd()returns the current working directory.nameidentifies the operating system.
Program Starts
↓
Import Statement
↓
Python Searches Module
↓
Module Found
↓
Module Loaded
↓
Functions Available
↓
Required Function Executed
↓
Output Displayed
↓
Program Ends
Python Program
│
├── Main Module
│
├── math Module
│
├── random Module
│
└── calculator Module
│
├── Variables
├── Functions
└── Classes
↓
Program Ends
- Encourages code reuse.
- Makes applications modular.
- Reduces duplicate code.
- Improves readability.
- Simplifies maintenance.
- Place import statements at the beginning of the file.
- Import only the required modules or members.
- Prefer explicit imports over wildcard imports.
- Use meaningful names for custom modules.
- Avoid naming custom modules after built-in modules.
Using a function without importing its module.
Using wildcard imports unnecessarily.
Misspelling the module name.
Naming custom modules after built-in modules such as:
math.py
random.py
os.py
This can prevent the intended built-in module from being imported.
It loads a module into the current program so that its members can be used.
import mathand
from math import sqrtimport math imports the entire module.
from math import sqrt imports only the sqrt() function.
Yes.
A Python program can import multiple built-in and user-defined modules.
Python raises
ModuleNotFoundErrorBecause they can cause namespace conflicts and reduce code readability.
At the beginning of the Python file, following any module-level documentation and before executable code.
Yes.
As long as they are located in Python's module search path.
- Import the
mathmodule and calculate a square root. - Import only the
factorial()function. - Import multiple functions from the same module.
- Create and import a custom module.
- Demonstrate
ModuleNotFoundError.
import modulefrom module import memberfrom module import member1, member2from module import *(avoid in production code)- Import statements should be placed at the top of the file.
- Python searches the module search path before importing.
In this chapter, you learned:
- Import Statement
- Importing Entire Modules
- Importing Specific Members
- Importing Multiple Members
- Wildcard Imports
- User-Defined Module Imports
- Module Search Process
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision