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. Here are some code examples that demonstrate how to use multiple modules with Python import statement:

Suppose you have two modules module1.py and module2.py that contain some functions:

Example

#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") 

Output

"Hello, John!"
"Hello, Jane!"

You can also use the as keyword to give a module a different name when you import it.

Example

In this example, we imported module1 as m1 and module2 as m2. This allows us to use a shorter name when we call the functions defined in these modules.

import module1 as m1
import module2 as m2

m1.say_hello("John")     # prints "Hello, John!"
m2.say_goodbye("Jane")    # prints "Goodbye, Jane!"

You can also use the from ... import ... statement to import specific functions or variables from a module. Here's an example:

Example

In this example, we imported only the say_hello function from module1 and only the say_goodbye function from module2. This allows us to use these functions directly without using the module name as a prefix.

from module1 import say_hello
from module2 import say_goodbye

say_hello("John")     # prints "Hello, John!"
say_goodbye("Jane")    # prints "Goodbye, Jane!"

Suppose you have three modules named maths.py, statistics.py, and geometry.py that contain functions related to math, statistics, and geometry respectively:

Example

#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):
    numbers.sort()
    mid = len(numbers) // 2
    if len(numbers) % 2 == 0:
        return (numbers[mid - 1] + numbers[mid]) / 2
    else:
        return numbers[mid]

#geometry.py

def area_of_circle(radius):
    return 3.14 * radius * radius
def perimeter_of_rectangle(length, breadth):
    return 2 * (length + breadth)

To use these modules in another Python program, you can import them using the import statement:

import maths
import statistics
import geometry

# calling functions from maths module
print(maths.add(5, 7))   
print(maths.subtract(10, 3))    

# calling functions from statistics module
numbers = [2, 4, 6, 8, 10]
print(statistics.mean(numbers))   
print(statistics.median(numbers))   

# calling functions from geometry module
print(geometry.area_of_circle(5))    
print(geometry.perimeter_of_rectangle(4, 6))    

Output

12
7
6.0
6
78.5
20

You can also use the from ... import ... statement to import specific functions from a module.

Example

In this example, we imported only the add and subtract functions from maths, the mean function from statistics, and the perimeter_of_rectangle function from geometry.

from maths import add, subtract
from statistics import mean
from geometry import perimeter_of_rectangle

print(add(5, 7))    # prints 12
print(subtract(10, 3))    # prints 7
numbers = [2, 4, 6, 8, 10]
print(mean(numbers))    # prints 6.0
print(perimeter_of_rectangle(4, 6))    # prints 20

Output

12
7

6.0
20

You can also use the as keyword to give a function a different name when you import it.

Example

In this example, we import add as addition, subtract as subtraction, mean as average, and perimeter_of_rectangle as rectangle_perimeter.

from maths import add as addition, subtract as subtraction
from statistics import mean as average
from geometry import perimeter_of_rectangle as rectangle_perimeter

print(addition(5, 7))    
print(subtraction(10, 3))    
numbers = [2, 4, 6, 8, 10]
print(average(numbers))    
print(rectangle_perimeter(4, 6))    

Output

12
7
6.0
20

Updated on: 11-Aug-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements