如何使用 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+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告