Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use multiple modules with Python import Statement?
In Python, you can use the import statement to use functions or variables defined in another module. Python provides several ways to import multiple modules, each with its own advantages for different scenarios.
Basic Module Import
Suppose you have two modules module1.py and module2.py that contain some functions −
# module1.py
def say_hello(name):
print("Hello, " + name + "!")
# module2.py
def say_goodbye(name):
print("Goodbye, " + name + "!")
To use these modules in another Python program, you can import them using the import statement −
import module1
import module2
module1.say_hello("John")
module2.say_goodbye("Jane")
Hello, John! Goodbye, Jane!
Using Aliases with 'as' Keyword
You can use the as keyword to give a module a different name when you import it. This allows you to use a shorter name when calling functions −
import module1 as m1
import module2 as m2
m1.say_hello("John")
m2.say_goodbye("Jane")
Hello, John! Goodbye, Jane!
Importing Specific Functions
You can use the from ... import ... statement to import specific functions or variables from a module. This allows you to use these functions directly without the module name prefix −
from module1 import say_hello
from module2 import say_goodbye
say_hello("John")
say_goodbye("Jane")
Hello, John! Goodbye, Jane!
Real-World Example with Multiple Modules
Let's create three modules for mathematical operations −
# maths.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
# statistics.py
def mean(numbers):
return sum(numbers) / len(numbers)
def median(numbers):
sorted_nums = sorted(numbers)
mid = len(sorted_nums) // 2
if len(sorted_nums) % 2 == 0:
return (sorted_nums[mid - 1] + sorted_nums[mid]) / 2
else:
return sorted_nums[mid]
# geometry.py
def area_of_circle(radius):
return 3.14159 * radius * radius
def perimeter_of_rectangle(length, breadth):
return 2 * (length + breadth)
Now import and use these modules −
import maths
import statistics
import geometry
# Using functions from maths module
print("Addition:", maths.add(5, 7))
print("Subtraction:", maths.subtract(10, 3))
# Using functions from statistics module
numbers = [2, 4, 6, 8, 10]
print("Mean:", statistics.mean(numbers))
print("Median:", statistics.median(numbers))
# Using functions from geometry module
print("Circle area:", geometry.area_of_circle(5))
print("Rectangle perimeter:", geometry.perimeter_of_rectangle(4, 6))
Addition: 12 Subtraction: 7 Mean: 6.0 Median: 6 Circle area: 78.53975 Rectangle perimeter: 20
Selective Import with Aliases
You can combine selective importing with aliases for better code readability −
from maths import add as addition, subtract as subtraction
from statistics import mean as average
from geometry import perimeter_of_rectangle as rect_perimeter
print("Sum:", addition(15, 25))
print("Difference:", subtraction(20, 8))
data = [1, 3, 5, 7, 9]
print("Average:", average(data))
print("Perimeter:", rect_perimeter(3, 4))
Sum: 40 Difference: 12 Average: 5.0 Perimeter: 14
Import Methods Comparison
| Method | Syntax | Namespace | Best For |
|---|---|---|---|
| Basic Import | import module |
Preserved | Avoiding name conflicts |
| Import with Alias | import module as alias |
Custom | Shorter names |
| Selective Import | from module import function |
Direct access | Using few functions |
| Selective with Alias | from module import func as alias |
Custom direct | Renaming functions |
Conclusion
Python offers flexible ways to import multiple modules using import, from...import, and as keywords. Choose the method based on your namespace requirements and code clarity needs.
