如何使用Python import語句匯入多個模組?


在Python中,可以使用import語句來使用在另一個模組中定義的函式或變數。以下是一些程式碼示例,演示如何使用Python import語句匯入多個模組。

假設您有兩個模組module1.py和module2.py,它們包含一些函式。

示例

#module1.py
def say_hello(name):
    print("Hello, " + name + "!")
#module2.py
def say_goodbye(name):
    print("Goodbye, " + name + "!")

要在另一個Python程式中使用這些模組,您可以使用import語句匯入它們。

import module1
import module2
module1.say_hello("John")     
module2.say_goodbye("Jane") 

輸出

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

您還可以使用as關鍵字在匯入模組時為其指定不同的名稱。

示例

在這個例子中,我們將module1匯入為m1,將module2匯入為m2。這樣,在呼叫這些模組中定義的函式時,可以使用較短的名稱。

import module1 as m1
import module2 as m2

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

您還可以使用from ... import ... 語句從模組中匯入特定的函式或變數。這是一個例子

示例

在這個例子中,我們只從module1匯入了say_hello函式,只從module2匯入了say_goodbye函式。這樣可以直接使用這些函式,而無需使用模組名稱作為字首。

from module1 import say_hello
from module2 import say_goodbye

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

假設您有三個名為maths.py、statistics.py和geometry.py的模組,它們分別包含與數學、統計和幾何相關的函式。

示例

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

要在另一個Python程式中使用這些模組,您可以使用import語句匯入它們。

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

輸出

12
7
6.0
6
78.5
20

您還可以使用from ... import ... 語句從模組中匯入特定的函式。

示例

在這個例子中,我們只從maths匯入了add和subtract函式,從statistics匯入了mean函式,從geometry匯入了perimeter_of_rectangle函式。

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

輸出

12
7

6.0
20

您還可以使用as關鍵字在匯入函式時為其指定不同的名稱。

示例

在這個例子中,我們將add匯入為addition,subtract匯入為subtraction,mean匯入為average,perimeter_of_rectangle匯入為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))    

輸出

12
7
6.0
20

更新於:2023年8月11日

7K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告