當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python test.support.import_helper.import_fresh_module用法及代碼示例

用法:

test.support.import_helper.import_fresh_module(name, fresh=(), blocked=(), deprecated=False)

此函數通過在導入之前從sys.modules 中刪除命名模塊來導入並返回命名 Python 模塊的新副本。請注意,與 reload() 不同,原始模塊不受此操作的影響。

fresh 是一個可迭代的附加模塊名稱,這些名稱在導入之前也會從 sys.modules 緩存中刪除。

blocked 是一個可迭代的模塊名稱,在導入期間在模塊緩存中替換為 None 以確保嘗試導入它們會引發 ImportError

在開始導入之前保存命名模塊以及在 freshblocked 參數中命名的任何模塊,然後在全新導入完成後重新插入 sys.modules

如果 deprecatedTrue ,則在此導入期間會抑製模塊和包棄用消息。

如果無法導入命名模塊,此函數將引發 ImportError

示例使用:

# Get copies of the warnings module for testing without affecting the
# version being used by the rest of the test suite. One copy uses the
# C implementation, the other is forced to use the pure Python fallback
# implementation
py_warnings = import_fresh_module('warnings', blocked=['_warnings'])
c_warnings = import_fresh_module('warnings', fresh=['_warnings'])

3.1 版中的新函數。

相關用法


注:本文由純淨天空篩選整理自python.org大神的英文原創作品 test.support.import_helper.import_fresh_module。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。