Quy tắc_import và tổ chức_module trong_Python

1. Định nghĩa cơ bản

Module: Là một file Python có phần mở rộng .py. Ví dụ: File tên là test.py sẽ tương ứng với module tên là test. Module giúp tổ chức code logic (biến, hàm, lớp, v.v.) để thực hiện một chức năng特定.

Package: Là cách để tổ chức các module một cách logic. Thực chất, package là một thư mục chứa các module và cần phải có file __init__.py để Python nhận biết đây là một package.

2. Các phương thức import

Phương thức 1: import module_name

Ví dụ:

Tạo một module có tên là module_test.py:

message = "LỖI"
def ghi_log():
    print("Đây là một hàm thử nghiệm log...")

def in_ra():
    print("Đây là một hàm thử nghiệm in...")

def doc_file():
    print("Đây là một hàm thử nghiệm đọc file...")

Import module_test vào file test_file1.py:

import module_test
print(module_test.message)
module_test.doc_file()

Kết quả khi chạy:

LỖI
Đây là một hàm thử nghiệm đọc file...

Process finished with exit code 0

Phương thức 2: from module_name import *

Ví dụ:

Import module_test vào file test_file2.py:

from module_test import *

print(message)  # Khi import bằng cách này, không cần thêm module_name.hàm()
ghi_log()

Nếu thêm một hàm cùng tên vào file test_file2.py:

from module_test import *

print(message)  # Import bằng cách này sẽ sử dụng hàm trực tiếp trong module
ghi_log()
in_ra()
def in_ra():
    print("Đây là hàm in từ test_file2.py")

in_ra()

Kết quả khi chạy:

LỖI
Đây là một hàm thử nghiệm log...
Đây là một hàm thử nghiệm in...
Đây là hàm in từ test_file2.py

Process finished with exit code 0

Như vậy, khi sử dụng from module_name import *, nếu có hàm cùng tên trong file hiện tại, hàm đó sẽ overwrite hàm trong module. Vì vậy, cách import này không được khuyến khích khi có khả năng xung đột tên.

Phương thức 3: from module_name import function as alias_name

Ví dụ:

Import hàm in_ra từ module_test vào file test_file3.py:

from module_test import in_ra as ham_in_ra

def in_ra():
    print("Đây là hàm in từ test_file3.py")

in_ra()
ham_in_ra()

Kết quả khi chạy:

Đây là hàm in từ test_file3.py
Đây là một hàm thử nghiệm in...

Process finished with exit code 0

Cách này sử dụng alias để gọi hàm từ module mà không gây xung đột tên.

3. Cơ chế hoạt động của import

Khi import một module, Python sẽ chạy và giải thích toàn bộ code trong file đó một lần. Tất cả các biến, hàm và lớp được khai báo trong module sẽ trở thành phần của module.

Khi import một package, Python sẽ chạy file __init__.py trong thư mục của package để xác định nội dung của package.

4. Quản lý đường dẫn import

Để import module từ một thư mục khác, cần thêm đường dẫn của thư mục đó vào biếnsys.path.

Ví dụ:

import sys
import os

# In ra các đường dẫn hiện tại
print(sys.path)

# Đường dẫn tuyệt đối của file hiện tại
print(os.path.abspath(__file__))

# Đường dẫn của thư mục chứa file hiện tại
print(os.path.dirname(os.path.abspath(__file__)))

# Đường dẫn của thư mục cha
print(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

Trong trường hợp cần import module từ một thư mục con:

x = os.path.dirname(os.path.dirname(__file__))
sys.path.append(x + "/module_test")

Bằng cách này, module_test sẽ được tìm thấy và import từ thư mục module_test.

Thẻ: python module import package path

Đăng vào ngày 19 tháng 7 lúc 08:22