Python tải lại biến toàn cầu

Mô-đun này cung cấp giao diện cho các cơ chế được sử dụng để thực hiện câu lệnh. Nó định nghĩa các hằng số và hàm sau

imp. get_magic[]

Trả về giá trị chuỗi ma thuật được sử dụng để nhận dạng tệp mã được biên dịch theo byte [tệp

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
3]. [Giá trị này có thể khác nhau đối với từng phiên bản Python. ]

Không dùng nữa kể từ phiên bản 3. 4. Sử dụng thay thế.

imp. get_suffixes[]

Trả về danh sách các bộ 3 phần tử, mỗi bộ mô tả một loại mô-đun cụ thể. Mỗi bộ ba có dạng

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
4, trong đó hậu tố là một chuỗi được thêm vào tên mô-đun để tạo thành tên tệp cần tìm kiếm, chế độ là chuỗi chế độ để chuyển đến hàm tích hợp để mở tệp [có thể là
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
0 cho

Không dùng nữa kể từ phiên bản 3. 3. Thay vào đó, hãy sử dụng các hằng số được xác định trên.

imp. find_module[tên[ , đường dẫn]]

Cố gắng tìm tên mô-đun. Nếu đường dẫn bị bỏ qua hoặc

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
6, danh sách các tên thư mục do
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
7 cung cấp sẽ được tìm kiếm, nhưng trước tiên, một số vị trí đặc biệt sẽ được tìm kiếm. chức năng cố gắng tìm một mô-đun tích hợp có tên đã cho [], sau đó là một mô-đun cố định [] và trên một số hệ thống, một số vị trí khác cũng được tìm kiếm [trên Windows, nó tìm trong sổ đăng ký có thể trỏ đến một

Mặt khác, đường dẫn phải là một danh sách các tên thư mục; . Tên không hợp lệ trong danh sách sẽ bị bỏ qua [nhưng tất cả các mục trong danh sách phải là chuỗi]

Nếu tìm kiếm thành công, giá trị trả về là bộ 3 phần tử

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
51

tệp là một vị trí mở ở đầu, tên đường dẫn là tên đường dẫn của tệp được tìm thấy và mô tả là một bộ 3 phần tử như được chứa trong danh sách được trả về bằng cách mô tả loại mô-đun được tìm thấy

Nếu mô-đun được tích hợp sẵn hoặc bị đóng băng thì tệp và tên đường dẫn đều là

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
6 và bộ mô tả chứa các chuỗi trống cho hậu tố và chế độ của nó; . Nếu tìm kiếm không thành công, được nâng lên. Các ngoại lệ khác cho biết các vấn đề với đối số hoặc môi trường

Nếu mô-đun là một gói, tệp là

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
6, tên đường dẫn là đường dẫn gói và mục cuối cùng trong bộ mô tả là

Chức năng này không xử lý tên mô-đun phân cấp [tên chứa dấu chấm]. Để tìm P. M, nghĩa là mô hình con M của gói P, sử dụng và để tìm và tải gói P, sau đó sử dụng với đối số đường dẫn được đặt thành

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
50. Khi bản thân P có tên chấm, hãy áp dụng đệ quy công thức này

Không dùng nữa kể từ phiên bản 3. 3. Sử dụng thay thế trừ khi Python 3. 3 khả năng tương thích là bắt buộc, trong trường hợp đó sử dụng. Ví dụ về cách sử dụng trường hợp cũ, xem phần tài liệu.

imp. load_module[tên , tệp , tên đường dẫn , mô tả]

Tải một mô-đun đã được tìm thấy trước đó bởi [hoặc bởi một tìm kiếm được tiến hành khác mang lại kết quả tương thích]. Chức năng này không chỉ nhập mô-đun. nếu mô-đun đã được nhập, nó sẽ tải lại mô-đun. Đối số tên cho biết tên mô-đun đầy đủ [bao gồm tên gói, nếu đây là mô-đun con của gói]. Đối số tệp là một tệp đang mở và tên đường dẫn là tên tệp tương ứng; . Đối số mô tả là một bộ, như sẽ được trả về bởi , mô tả loại mô-đun nào phải được tải

Nếu tải thành công, giá trị trả về là đối tượng mô-đun;

Quan trọng. người gọi chịu trách nhiệm đóng đối số tệp, nếu nó không phải là

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
6, ngay cả khi một ngoại lệ được đưa ra. Điều này được thực hiện tốt nhất bằng cách sử dụng câu lệnh …

Không dùng nữa kể từ phiên bản 3. 3. Nếu trước đây được sử dụng kết hợp với thì hãy cân nhắc sử dụng , nếu không, hãy sử dụng trình tải được trả về bởi trình thay thế mà bạn đã chọn. Nếu bạn đã gọi và các hàm liên quan trực tiếp với các đối số đường dẫn tệp thì hãy sử dụng kết hợp và. Xem phần tài liệu để biết chi tiết về các phương pháp khác nhau.

imp. new_module[tên]

Trả về một đối tượng mô-đun trống mới có tên. Đối tượng này không được chèn vào

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
89

Không dùng nữa kể từ phiên bản 3. 4. Sử dụng thay thế.

imp. tải lại[mô-đun]

Tải lại mô-đun đã nhập trước đó. Đối số phải là một đối tượng mô-đun, vì vậy nó phải được nhập thành công trước đó. Điều này hữu ích nếu bạn đã chỉnh sửa tệp nguồn mô-đun bằng trình chỉnh sửa bên ngoài và muốn dùng thử phiên bản mới mà không cần rời khỏi trình thông dịch Python. Giá trị trả về là đối tượng mô-đun [giống như đối số mô-đun]

Khi

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
91 được thực thi

  • Mã của mô-đun Python được biên dịch lại và mã cấp mô-đun được thực thi lại, xác định một tập hợp đối tượng mới được liên kết với tên trong từ điển của mô-đun. Chức năng

    import imp
    import sys
    
    def __import__[name, globals=None, locals=None, fromlist=None]:
        # Fast path: see if the module has already been imported.
        try:
            return sys.modules[name]
        except KeyError:
            pass
    
        # If any of the following calls raises an exception,
        # there's a problem we can't handle -- let the caller handle it.
    
        fp, pathname, description = imp.find_module[name]
    
        try:
            return imp.load_module[name, fp, pathname, description]
        finally:
            # Since we may exit via an exception, close fp explicitly.
            if fp:
                fp.close[]
    
    92 của mô-đun mở rộng không được gọi lần thứ hai

  • Như với tất cả các đối tượng khác trong Python, các đối tượng cũ chỉ được lấy lại sau khi số tham chiếu của chúng giảm xuống 0

  • Tên trong không gian tên mô-đun được cập nhật để trỏ đến bất kỳ đối tượng mới hoặc thay đổi nào

  • Các tham chiếu khác đến các đối tượng cũ [chẳng hạn như tên bên ngoài mô-đun] không được phục hồi để tham chiếu đến các đối tượng mới và phải được cập nhật trong mỗi không gian tên nơi chúng xuất hiện nếu muốn

Có một số lưu ý khác

Khi một mô-đun được tải lại, từ điển của nó [chứa các biến toàn cục của mô-đun] được giữ lại. Định nghĩa lại tên sẽ ghi đè định nghĩa cũ, vì vậy đây thường không phải là vấn đề. Nếu phiên bản mới của mô-đun không xác định tên đã được xác định bởi phiên bản cũ, thì định nghĩa cũ vẫn còn. Tính năng này có thể được sử dụng để tạo lợi thế cho mô-đun nếu nó duy trì một bảng toàn cầu hoặc bộ đệm của các đối tượng — với một câu lệnh, nó có thể kiểm tra sự hiện diện của bảng và bỏ qua quá trình khởi tạo nếu muốn

________số 8

Việc tải lại các mô-đun tích hợp hoặc được tải động là hợp pháp, ngoại trừ và. Tuy nhiên, trong nhiều trường hợp, các mô-đun mở rộng không được thiết kế để khởi tạo nhiều lần và có thể bị lỗi theo các cách tùy ý khi tải lại

Nếu một mô-đun nhập các đối tượng từ một mô-đun khác bằng cách sử dụng … …, thì việc gọi mô-đun kia không xác định lại các đối tượng được nhập từ nó — một cách để giải quyết vấn đề này là thực hiện lại câu lệnh

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
97, một cách khác là sử dụng
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
9 và các tên đủ điều kiện [mô-đun. *tên*] thay vào đó

Nếu một mô-đun khởi tạo các thể hiện của một lớp, thì việc tải lại mô-đun định nghĩa lớp đó không ảnh hưởng đến các định nghĩa phương thức của các thể hiện — chúng tiếp tục sử dụng định nghĩa lớp cũ. Điều này cũng đúng với các lớp dẫn xuất

Đã thay đổi trong phiên bản 3. 3. Dựa vào cả

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
32 và
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
33 được xác định trên mô-đun đang được tải lại thay vì chỉ
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
32.

Không dùng nữa kể từ phiên bản 3. 4. Sử dụng thay thế.

Các chức năng sau đây là các tiện ích để xử lý các đường dẫn tệp được biên dịch theo byte PEP 3147

Mới trong phiên bản 3. 2

imp. cache_from_source[đường dẫn , debug_override=Không có]

Trả lại đường dẫn PEP 3147 cho tệp được biên dịch theo byte được liên kết với đường dẫn nguồn. Ví dụ: nếu đường dẫn là

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
36 thì giá trị trả về sẽ là
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
37 cho Python 3. 2. Chuỗi
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
38 xuất phát từ thẻ ma thuật hiện tại [xem ; nếu
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
90 không được xác định thì sẽ được nâng lên]. Bằng cách chuyển vào
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
92 hoặc
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
93 cho debug_override, bạn có thể ghi đè giá trị của hệ thống cho
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
94, dẫn đến mã byte được tối ưu hóa

con đường không cần tồn tại

Đã thay đổi trong phiên bản 3. 3. Nếu

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
90 là
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
6 thì tăng.

Không dùng nữa kể từ phiên bản 3. 4. Sử dụng thay thế.

Đã thay đổi trong phiên bản 3. 5. Tham số debug_override không còn tạo tệp

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
99 nữa.

imp. source_from_cache[đường dẫn]

Cung cấp đường dẫn đến tên tệp PEP 3147, trả lại đường dẫn tệp mã nguồn được liên kết. Ví dụ: nếu đường dẫn là

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
37 thì đường dẫn được trả về sẽ là
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
36. đường dẫn không cần tồn tại, tuy nhiên nếu nó không phù hợp với định dạng PEP 3147, a sẽ tăng lên. Nếu
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
90 không được xác định, được nâng lên

Đã thay đổi trong phiên bản 3. 3. Tăng khi không xác định được

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
90.

Không dùng nữa kể từ phiên bản 3. 4. Sử dụng thay thế.

imp. get_tag[]

Trả về chuỗi thẻ ma thuật PEP 3147 khớp với phiên bản số ma thuật này của Python, như được trả về bởi

Không dùng nữa kể từ phiên bản 3. 4. Sử dụng trực tiếp

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
90 bắt đầu bằng Python 3. 3.

Các chức năng sau giúp tương tác với cơ chế khóa bên trong của hệ thống nhập. Khóa ngữ nghĩa của nhập khẩu là một chi tiết triển khai có thể thay đổi từ bản phát hành này sang bản phát hành khác. Tuy nhiên, Python đảm bảo rằng việc nhập vòng tròn hoạt động mà không có bất kỳ bế tắc nào

imp. lock_held[]

Trả lại ________ 292 của khóa nhập khẩu toàn cầu hiện đang được giữ, nếu không thì ________ 293. Trên nền tảng không có chủ đề, luôn trả về

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
93

Trên các nền tảng có luồng, luồng thực thi thao tác nhập trước tiên sẽ giữ khóa nhập chung, sau đó thiết lập khóa theo mô-đun cho phần còn lại của quá trình nhập. Điều này chặn các luồng khác nhập cùng một mô-đun cho đến khi quá trình nhập ban đầu hoàn tất, ngăn các luồng khác nhìn thấy các đối tượng mô-đun chưa hoàn chỉnh được tạo bởi luồng ban đầu. Một ngoại lệ được tạo ra cho nhập khẩu vòng tròn, theo cách xây dựng, phải hiển thị một đối tượng mô-đun không đầy đủ tại một số điểm

Đã thay đổi trong phiên bản 3. 3. Kế hoạch khóa phần lớn đã thay đổi thành khóa theo từng mô-đun. Khóa nhập toàn cầu được giữ cho một số tác vụ quan trọng, chẳng hạn như khởi tạo khóa cho mỗi mô-đun.

Không dùng nữa kể từ phiên bản 3. 4

imp. acquire_lock[]

Nhận khóa nhập toàn cầu của trình thông dịch cho luồng hiện tại. Khóa này nên được sử dụng bởi móc nhập để đảm bảo an toàn luồng khi nhập mô-đun

Sau khi một luồng đã có khóa nhập, luồng đó có thể lấy lại khóa đó mà không bị chặn;

Trên các nền tảng không có chủ đề, chức năng này không làm gì cả

Đã thay đổi trong phiên bản 3. 3. Kế hoạch khóa phần lớn đã thay đổi thành khóa theo từng mô-đun. Khóa nhập toàn cầu được giữ cho một số tác vụ quan trọng, chẳng hạn như khởi tạo khóa cho mỗi mô-đun.

Không dùng nữa kể từ phiên bản 3. 4

imp. release_lock[]

Giải phóng khóa nhập toàn cầu của trình thông dịch. Trên các nền tảng không có chủ đề, chức năng này không làm gì cả

Đã thay đổi trong phiên bản 3. 3. Kế hoạch khóa phần lớn đã thay đổi thành khóa theo từng mô-đun. Khóa nhập toàn cầu được giữ cho một số tác vụ quan trọng, chẳng hạn như khởi tạo khóa cho mỗi mô-đun.

Không dùng nữa kể từ phiên bản 3. 4

Các hằng số có giá trị nguyên sau đây, được định nghĩa trong mô-đun này, được sử dụng để chỉ ra kết quả tìm kiếm của

imp. PY_SOURCE

Mô-đun được tìm thấy dưới dạng tệp nguồn

Không dùng nữa kể từ phiên bản 3. 3

imp. PY_COMPILED

Mô-đun được tìm thấy dưới dạng tệp đối tượng mã được biên dịch

Không dùng nữa kể từ phiên bản 3. 3

imp. C_EXTENSION

Mô-đun được tìm thấy dưới dạng thư viện chia sẻ có thể tải động

Không dùng nữa kể từ phiên bản 3. 3

imp. PKG_DIRECTORY

Mô-đun được tìm thấy dưới dạng thư mục gói

Không dùng nữa kể từ phiên bản 3. 3

imp. C_BUILTIN

Mô-đun được tìm thấy dưới dạng mô-đun tích hợp

Không dùng nữa kể từ phiên bản 3. 3

imp. PY_FROZEN

Mô-đun được tìm thấy dưới dạng mô-đun bị đóng băng

Không dùng nữa kể từ phiên bản 3. 3

lớp imp. NullImporter[path_string]

Loại này là móc nhập PEP 302 xử lý các chuỗi đường dẫn không phải thư mục do không tìm thấy bất kỳ mô-đun nào. Gọi loại này với một thư mục hiện có hoặc chuỗi trống sẽ tăng. Nếu không, một thể hiện được trả lại

Các trường hợp chỉ có một phương thức

find_module[tên đầy đủ[ , đường dẫn]]

Phương thức này luôn trả về

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
6, cho biết rằng không thể tìm thấy mô-đun được yêu cầu

Đã thay đổi trong phiên bản 3. 3. ______06 được chèn vào

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
39 thay vì một thể hiện của.

Không dùng nữa kể từ phiên bản 3. 4. Thay vào đó, hãy chèn

import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
6 vào
import imp
import sys

def __import__[name, globals=None, locals=None, fromlist=None]:
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module[name]

    try:
        return imp.load_module[name, fp, pathname, description]
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close[]
39.

ví dụ

Hàm sau mô phỏng câu lệnh nhập tiêu chuẩn cho đến Python 1. 4 [không có tên mô-đun phân cấp]. [Triển khai này sẽ không hoạt động trong phiên bản đó, vì đã được mở rộng và đã được thêm vào trong 1. 4. ]

Tải lại trong Python là gì?

Tải lại [] được được sử dụng để tải lại mô-đun đã nhập trước đó hoặc mô-đun đã tải . Điều này hữu ích trong trường hợp bạn liên tục chạy tập lệnh kiểm tra trong phiên tương tác, nó luôn sử dụng phiên bản đầu tiên của các mô-đun mà chúng tôi đang phát triển, ngay cả khi chúng tôi đã thực hiện các thay đổi đối với mã.

Bạn có thể thay đổi các biến toàn cục trong Python không?

Trong Python, từ khóa toàn cục cho phép chúng ta sửa đổi biến bên ngoài phạm vi hiện tại . Nó được sử dụng để tạo một biến toàn cục và thay đổi biến đó trong ngữ cảnh cục bộ.

Chủ Đề