5.模組引入系統

一個module 中的 Python 程式碼透過importing 的過程來存取另一個模組中的程式碼。import 陳述式是叫用 (invoke) 引入機制最常見的方法,但這不是唯一的方法。函式如importlib.import_module() 以及內建函式__import__() 也可以用來叫用引入機制。

import 陳述式結合了兩個操作:首先搜尋指定的模組,然後將搜尋結果繫結到本地作用域中的一個名稱。import 陳述式的搜尋操作被定義為一個對__import__() 函式的呼叫,並帶有相應的引數。__import__() 的回傳值用於執行import 陳述式的名稱繫結操作。有關名稱繫結操作的詳細資訊,請參見import 陳述式。

直接呼叫__import__() 只會執行模組搜尋操作,以及在找到時執行模組的建立操作。雖然某些副作用可能會發生,例如引入父套件 (parent package),以及更新各種快取(包括sys.modules),但只有import 陳述式會執行名稱繫結操作。

當執行import 陳述式時,會呼叫內建的__import__() 函式。其他叫用引入系統的機制(如importlib.import_module())可以選擇略過__import__(),並使用它們自己的解決方案來實作引入語意。

當模組首次被引入時,Python 會搜尋該模組,若找到則會建立一個模組物件[1],並對其進行初始化。如果找不到指定的模組,則會引發ModuleNotFoundError。當引入機制被叫用時,Python 會實作各種策略來搜尋指定的模組。這些策略可以透過使用以下章節描述的各種 hook(掛鉤)來修改和擴展。

在 3.3 版的變更:引入系統已被更新,以完全實作PEP 302 的第二階段。不再有隱式引入機制——完整的引入系統已透過sys.meta_path 公開。此外,原生命名空間套件支援(請參閱PEP 420)也已被實作。

5.1.importlib

importlib 模組提供了豐富的 API 來與引入系統互動。例如,importlib.import_module() 提供了一個比內建的__import__() 更推薦且更簡單的 API 來叫用引入機制。更多詳細資訊請參閱importlib 函式庫文件。

5.2.套件

Python 只有一種類型的模組物件,且所有模組,無論其是使用 Python、C 還是其他語言實作,都是這種類型。為了幫助組織模組並提供命名階層,Python 導入了套件的概念。

你可以將套件視為檔案系統中的目錄,模組則是目錄中的檔案,但不要過於字面地理解這個比喻,因為套件和模組不一定來自檔案系統。為了方便解釋,我們將使用這個目錄和檔案的比喻。就像檔案系統目錄一樣,套件是分層組織的,套件本身可以包含子套件以及一般模組。

請記住,所有的套件都是模組,但並非所有模組都是套件。換句話說,套件只是一種特殊的模組。具體來說,任何包含__path__ 屬性的模組都被視為套件。

所有模組都有一個名稱。子套件的名稱與其父套件名稱之間用一個點來分隔,類似於 Python 的標準屬性存取語法。因此,你可能會有一個名為email 的套件,該套件又有一個名為email.mime 的子套件,並且該子套件中有一個名為email.mime.text 的模組。

5.2.1.一般套件

Python 定義了兩種類型的套件,一般套件命名空間套件。一般套件是 Python 3.2 及更早版本中存在的傳統套件。一般套件通常實作成一個包含__init__.py 檔案的目錄。當引入一般套件時,該__init__.py 檔案會被隱式執行,其定義的物件會繫結到該套件的命名空間中的名稱。__init__.py 檔案可以包含與任何其他模組相同的 Python 程式碼,並且 Python 會在引入時為該模組增加一些額外的屬性。

例如,以下檔案系統布置定義了一個頂層的parent 套件,該套件包含三個子套件:

parent/__init__.pyone/__init__.pytwo/__init__.pythree/__init__.py

引入parent.one 將隱式執行parent/__init__.pyparent/one/__init__.py。隨後引入parent.twoparent.three 將分別執行parent/two/__init__.pyparent/three/__init__.py

5.2.2.命名空間套件

命名空間套件是由不同的部分 組成的,每個部分都為父套件提供一個子套件。這些部分可以位於檔案系統上的不同位置。部分可能也存在於壓縮檔案中、網路上,或 Python 在引入時搜尋的任何其他地方。命名空間套件不一定直接對應於檔案系統中的對象;它們可能是沒有具體表示的虛擬模組。

命名空間套件的__path__ 屬性不使用普通的串列。它們使用自定義的可疊代型別,當父套件的路徑(或頂層套件的sys.path)發生變化時,會在下一次引入嘗試時自動執行新一輪的套件部分搜尋。

在命名空間套件中,不存在parent/__init__.py 檔案。實際上,在引入搜尋過程中可能會找到多個parent 目錄,每個目錄由不同的部分提供。因此,parent/one 可能與parent/two 不會實際位於一起。在這種情況下,每當引入頂層parent 套件或其子套件之一時,Python 會為頂層parent 套件建立一個命名空間套件。

有關命名空間套件的規範,請參見PEP 420

5.3.搜尋

在開始搜尋之前,Python 需要被引入模組(或套件,但在本討論中,兩者的區別無關緊要)的完整限定名稱 (qualified name)。此名稱可能來自import 陳述式的各種引數,或來自importlib.import_module()__import__() 函式的參數。

此名稱將在引入搜尋的各個階段中使用,並且它可能是指向子模組的點分隔路徑,例如foo.bar.baz。在這種情況下,Python 會首先嘗試引入foo,然後是foo.bar,最後是foo.bar.baz。如果任何中間引入失敗,則會引發ModuleNotFoundError

5.3.1.模組快取

在引入搜尋過程中首先檢查的地方是sys.modules。此對映用作所有先前引入過的模組的快取,包括中間路徑。因此,如果foo.bar.baz 之前已被引入,sys.modules 將包含foofoo.barfoo.bar.baz 的條目。每個鍵的值都是相應的模組物件。

在引入過程中,會在sys.modules 中查找模組名稱,如果存在,則相關的值為滿足此引入的模組,此引入過程即完成。然而,如果值是None,則會引發ModuleNotFoundError。如果模組名稱不存在,Python 會繼續搜尋該模組。

sys.modules 是可寫入的。刪除一個鍵可能不會銷毀相關聯的模組(因為其他模組可能持有對它的參照),但會使指定的模組的快取條目失效,導致 Python 在下一次引入該模組時重新搜尋。也可以將鍵賦值為None,這會強制下一次引入該模組時引發ModuleNotFoundError

但請注意,如果你保留了對模組物件的參照,並在sys.modules 中使其快取條目失效,然後重新引入指定的模組,這兩個模組物件將不會相同。相比之下,importlib.reload() 會重用相同的模組物件,並透過重新執行模組的程式碼來簡單地重新初始化模組內容。

5.3.2.尋檢器 (Finder) 與載入器 (Loader)

如果在sys.modules 中找不到指定的模組,則會叫用 Python 的引入協定來尋找並載入該模組。這個協定由兩個概念性物件組成,尋檢器載入器。尋檢器的任務是使用其已知的策略來確定是否能找到命名模組。實作這兩個介面的物件稱為引入器 (importer) ——當它們發現可以載入所請求的模組時,會回傳它們自己。

Python 包含多個預設的尋檢器和引入器。第一個尋檢器知道如何定位內建模組,第二個尋檢器知道如何定位凍結模組。第三個預設尋檢器會在import path 中搜尋模組。import path 是一個位置的列表,這些位置可能是檔案系統路徑或壓縮檔案,也可以擴展以搜尋任何可定位的資源,例如由 URL 識別的資源。

引入機制是可擴展的,因此可以增加新的尋檢器來擴展模組搜尋的範圍和作用域。

尋檢器實際上不會載入模組。如果它們能找到指定的模組,它們會回傳一個模組規格,這是一個模組的引入相關資訊的封裝,引入機制會在載入模組時使用這些資訊。

以下各節將更詳細地描述尋檢器和載入器的協定,包括如何建立和註冊新的尋檢器和載入器來擴展引入機制。

在 3.4 版的變更:Python 在之前的版本中,尋檢器會直接回傳載入器,而現在它們回傳的是包含載入器的模組規格。載入器仍在引入過程中使用,但其責任減少了。

5.3.3.引入掛鉤 (Import hooks)

引入機制的設計是可擴展的;其主要機制是引入掛鉤。引入掛鉤有兩種類型:元掛鉤 (meta hooks)引入路徑掛鉤

元掛鉤會在引入處理的開始階段被呼叫,除了查找sys.modules 快取外,其他引入處理還未發生時就會呼叫。這允許元掛鉤覆蓋sys.path 的處理、凍結模組,甚至是內建模組。元掛鉤透過將新的尋檢器物件添加到sys.meta_path 中來註冊,具體描述請參閱以下段落。

引入路徑掛鉤被視為sys.path(或package.__path__)處理過程的一部分來呼叫,當遇到與其相關聯的路徑項目時就會被觸發。引入路徑掛鉤透過將新的可呼叫對象增加到sys.path_hooks 中來註冊,具體描述請參閱以下段落。

5.3.4.元路徑

當在sys.modules 中找不到命名模組時,Python 接下來會搜尋sys.meta_path,其中包含一個元路徑尋檢器物件串列。這些尋檢器會依次被查詢,看它們是否知道如何處理命名模組。元路徑尋檢器必須實作一個名為find_spec() 的方法,該方法接收三個引數:名稱、引入路徑和(可選的)目標模組。元路徑尋檢器可以使用任何策略來確定它是否能處理命名模組。

如果元路徑尋檢器知道如何處理命名模組,它會回傳一個規格物件。如果它無法處理命名模組,則回傳None。如果sys.meta_path 的處理到達串列的末尾仍未回傳規格,則會引發ModuleNotFoundError。任何其他引發的例外將直接向上傳播,並中止引入過程。

元路徑尋檢器的find_spec() 方法會以兩個或三個引數來呼叫。第一個是被引入模組的完全限定名稱,例如foo.bar.baz。第二個引數是用於模組搜尋的路徑條目。對於頂層模組,第二個引數是None,但對於子模組或子套件,第二個引數是父套件的__path__ 屬性的值。如果無法存取相應的__path__ 屬性,將引發ModuleNotFoundError。第三個引數是一個現有的模組物件,該物件將成為後續載入的目標。引入系統只會在重新載入時傳入目標模組。

對於一個引入請求,元路徑可能會被遍歷多次。例如,假設參與的模組都沒有被快取,則引入foo.bar.baz 將首先執行頂層引入,對每個元路徑尋檢器(mpf)呼叫mpf.find_spec("foo",None,None)。當foo 被引入後,將再次藉由遍歷元路徑引入foo.bar,並呼叫mpf.find_spec("foo.bar",foo.__path__,None)。當foo.bar 被引入後,最後一次遍歷會呼叫mpf.find_spec("foo.bar.baz",foo.bar.__path__,None)

一些元路徑尋檢器僅支援頂層引入。當第二個引數傳入None 以外的值時,這些引入器將始終回傳None

Python 的預設sys.meta_path 有三個元路徑尋檢器,一個知道如何引入內建模組,一個知道如何引入凍結模組,還有一個知道如何從import path 引入模組(即path based finder)。

在 3.4 版的變更:元路徑尋檢器的find_spec() 方法取代了find_module(),後者現在已被棄用。雖然它將繼續正常工作,但引入機制僅在尋檢器未實作find_spec() 時才會嘗試使用它。

在 3.10 版的變更:引入系統現在使用find_module() 時將引發ImportWarning

在 3.12 版的變更:find_module() 已被移除。請改用find_spec()

5.4.載入

如果找到模組規格,引入機制會在載入模組時使用該規格(以及它包含的載入器)。以下是引入過程中載入部分的大致情況:

module=Noneifspec.loaderisnotNoneandhasattr(spec.loader,'create_module'):# 這裡假設載入器上也會定義 'exec_module'module=spec.loader.create_module(spec)ifmoduleisNone:module=ModuleType(spec.name)# 與引入相關的模組屬性會在此處設定:_init_module_attrs(spec,module)ifspec.loaderisNone:# 不支援raiseImportErrorifspec.originisNoneandspec.submodule_search_locationsisnotNone:# 命名空間套件sys.modules[spec.name]=moduleelifnothasattr(spec.loader,'exec_module'):module=spec.loader.load_module(spec.name)else:sys.modules[spec.name]=moduletry:spec.loader.exec_module(module)exceptBaseException:try:delsys.modules[spec.name]exceptKeyError:passraisereturnsys.modules[spec.name]

請注意下列細節:

  • 如果sys.modules 中已存在具有給定名稱的模組物件,引入會已回傳該物件。

  • 在載入器執行模組程式碼之前,模組將已存在於sys.modules 中。這一點至關重要,因為模組程式碼可能會(直接或間接)引入自己;事先將其增加到sys.modules 可以預防類似無限遞迴以及多次重覆載入等情形。

  • 如果載入失敗,只有載入失敗的模組會從sys.modules 中刪除。任何已存在於sys.modules 快取中的模組,以及任何在載入失敗前成功載入的模組,都必須保留在快取中。此情形與重新載入不同,在重新載入時,即使載入失敗的模組也會保留在sys.modules 中。

  • 模組建立後、在執行之前,引入機制會設置與引入相關的模組屬性(在上面的偽程式碼範例中為 "_init_module_attrs"),具體內容在之後的段落會總結。

  • 模組執行是載入過程中的關鍵時刻,此時模組的命名空間會被新增名稱。執行過程完全交由載入器處理,由其決定如何新增以及新增什麼。

  • 在載入過程中建立並傳遞給 exec_module() 的模組,可能不會是引入結束時回傳的模組[2]

在 3.4 版的變更:引入系統已接管載入器的模板 (boilerplate) 責任。之前是由importlib.abc.Loader.load_module() 方法執行的。

5.4.1.載入器

模組載入器提供了載入的關鍵功能:模組執行。引入機制會以單一引數(即要執行的模組物件)呼叫importlib.abc.Loader.exec_module() 方法。任何從exec_module() 回傳的值都會被忽略。

載入器必須滿足以下要求:

  • 如果模組是 Python 模組(而非內建模組或動態載入的擴充),載入器應在模組的全域命名空間 (module.__dict__) 中執行該模組的程式碼。

  • 如果載入器無法執行該模組,應引發ImportError。不過,在exec_module() 中引發的任何其他例外也會被傳播。

在許多情況下,尋檢器和載入器可以是同一個物件;在這種情況下,find_spec() 方法只需回傳一個載入器設為self 的規格即可。

模組載入器可以選擇透過實作create_module() 方法,在載入過程中建立模組物件。該方法接受一個引數,即模組規格,並回傳在載入過程中要使用的新的模組物件。create_module() 不需要在模組物件上設定任何屬性。如果該方法回傳None,引入機制將自行建立新的模組。

在 3.4 版被加入:載入器的create_module() 方法。

在 3.4 版的變更:load_module() 方法已被exec_module() 取代,引入機制已承擔所有載入的模板責任。

為了與現有的載入器相容,引入機制會在載入器未實作exec_module() 且存在load_module() 方法時使用該方法。然而,load_module() 已被棄用,載入器應改為實作exec_module()

load_module() 方法除了執行模組外,還必須實作上述全部的模板載入功能。所有相同的限制依然適用,並且還有一些額外的說明:

  • 如果sys.modules 中已存在具有給定名稱的模組物件,載入器必須使用該模組(否則importlib.reload() 將無法正常運作)。如果命名模組不存在於sys.modules 中,載入器必須建立一個新的模組物件並將其新增至sys.modules

  • 在載入器執行模組程式碼之前,該模組必須已存在於sys.modules 中,以防止無限遞迴或多次載入。

  • 如果載入失敗,載入器必須移除已經插入到sys.modules 中的任何模組,但只能移除失敗的模組(們),且僅在載入器本身明確載入這些模組時才需移除。

在 3.5 版的變更:exec_module() 已定義但未定義create_module() 時,將引發DeprecationWarning

在 3.6 版的變更:exec_module() 已定義但未定義create_module() 時,將引發ImportError

在 3.10 版的變更:使用load_module() 將引發ImportWarning

5.4.2.子模組

當使用任何機制(例如importlib APIs、importimport-from 陳述式,或內建的__import__())載入子模組時,會將子模組物件繫結到父模組的命名空間中。例如,如果套件spam 有一個子模組foo,則在引入spam.foo 之後,spam 將擁有一個名為foo 的屬性,該屬性繫結到子模組。我們假設你有以下的目錄結構:

spam/__init__.pyfoo.py

並且spam/__init__.py 中包含以下程式碼:

from.fooimportFoo

那麼執行以下程式碼會將fooFoo 的名稱繫結到spam 模組中:

>>>importspam>>>spam.foo<module 'spam.foo' from '/tmp/imports/spam/foo.py'>>>>spam.Foo<class 'spam.foo.Foo'>

鑑於 Python 相似的名稱繫結規則,這可能看起來有些出人意料,但這實際上是引入系統的一個基本特性。不變的是如果你擁有sys.modules['spam']sys.modules['spam.foo'](就像上述引入後那樣),那麼後者必須作為前者的foo 屬性出現。

5.4.3.模組規格

引入機制在引入過程中使用有關每個模組的各種資訊,尤其是在載入之前。大多數資訊對所有模組來說都是通用的。模組規格的目的是以每個模組為基礎封裝這些與引入相關的資訊。

在引入過程中使用規格允許在引入系統的各個組件之間傳遞狀態,例如在建立模組規格的尋檢器和執行該規格的載入器之間傳遞。最重要的是,這允許引入機制執行載入的模板操作,而在沒有模組規格的情況下,這些操作則是載入器的責任。

模組的規格以module.__spec__ 的形式公開。適當地設定__spec__ 同樣適用於在直譯器啟動期間初始化的模組。唯一的例外是__main__,其中__spec__在某些情況下被設定成 None

有關模組規格內容的詳細資訊,請參閱ModuleSpec

在 3.4 版被加入.

5.4.4.__path__ attributes on modules

The__path__ attribute should be a (possibly empty)sequence of strings enumerating the locations where the package'ssubmodules will be found. By definition, if a module has a__path__attribute, it is apackage.

A package's__path__ attribute is used during imports of itssubpackages.Within the import machinery, it functions much the same assys.path,i.e. providing a list of locations to search for modules during import.However,__path__ is typically much more constrained thansys.path.

The same rules used forsys.path also apply to a package's__path__.sys.path_hooks (described below) areconsulted when traversing a package's__path__.

A package's__init__.py file may set or alter the package's__path__attribute, and this was typically the way namespace packages were implementedprior toPEP 420. With the adoption ofPEP 420, namespace packages nolonger need to supply__init__.py files containing only__path__manipulation code; the import machinery automatically sets__path__correctly for the namespace package.

5.4.5.Module reprs

By default, all modules have a usable repr, however depending on theattributes set above, and in the module's spec, you can more explicitlycontrol the repr of module objects.

If the module has a spec (__spec__), the import machinery will tryto generate a repr from it. If that fails or there is no spec, the importsystem will craft a default repr using whatever information is availableon the module. It will try to use themodule.__name__,module.__file__, andmodule.__loader__ as input into the repr,with defaults for whatever information is missing.

Here are the exact rules used:

  • If the module has a__spec__ attribute, the information in the specis used to generate the repr. The "name", "loader", "origin", and"has_location" attributes are consulted.

  • If the module has a__file__ attribute, this is used as part of themodule's repr.

  • If the module has no__file__ but does have a__loader__ that is notNone, then the loader's repr is used as part of the module's repr.

  • Otherwise, just use the module's__name__ in the repr.

在 3.12 版的變更:Use ofmodule_repr(), having been deprecated since Python 3.4, wasremoved in Python 3.12 and is no longer called during the resolution of amodule's repr.

5.4.6.Cached bytecode invalidation

Before Python loads cached bytecode from a.pyc file, it checks whether thecache is up-to-date with the source.py file. By default, Python does thisby storing the source's last-modified timestamp and size in the cache file whenwriting it. At runtime, the import system then validates the cache file bychecking the stored metadata in the cache file against the source'smetadata.

Python also supports "hash-based" cache files, which store a hash of the sourcefile's contents rather than its metadata. There are two variants of hash-based.pyc files: checked and unchecked. For checked hash-based.pyc files,Python validates the cache file by hashing the source file and comparing theresulting hash with the hash in the cache file. If a checked hash-based cachefile is found to be invalid, Python regenerates it and writes a new checkedhash-based cache file. For unchecked hash-based.pyc files, Python simplyassumes the cache file is valid if it exists. Hash-based.pyc filesvalidation behavior may be overridden with the--check-hash-based-pycsflag.

在 3.7 版的變更:Added hash-based.pyc files. Previously, Python only supportedtimestamp-based invalidation of bytecode caches.

5.5.The Path Based Finder

As mentioned previously, Python comes with several default meta path finders.One of these, called thepath based finder(PathFinder), searches animport path,which contains a list ofpath entries. Each pathentry names a location to search for modules.

The path based finder itself doesn't know how to import anything. Instead, ittraverses the individual path entries, associating each of them with apath entry finder that knows how to handle that particular kind of path.

The default set of path entry finders implement all the semantics for findingmodules on the file system, handling special file types such as Python sourcecode (.py files), Python byte code (.pyc files) andshared libraries (e.g..so files). When supported by thezipimportmodule in the standard library, the default path entry finders also handleloading all of these file types (other than shared libraries) from zipfiles.

Path entries need not be limited to file system locations. They can refer toURLs, database queries, or any other location that can be specified as astring.

The path based finder provides additional hooks and protocols so that youcan extend and customize the types of searchable path entries. For example,if you wanted to support path entries as network URLs, you could write a hookthat implements HTTP semantics to find modules on the web. This hook (acallable) would return apath entry finder supporting the protocoldescribed below, which was then used to get a loader for the module from theweb.

A word of warning: this section and the previous both use the termfinder,distinguishing between them by using the termsmeta path finder andpath entry finder. These two types of finders are very similar,support similar protocols, and function in similar ways during the importprocess, but it's important to keep in mind that they are subtly different.In particular, meta path finders operate at the beginning of the importprocess, as keyed off thesys.meta_path traversal.

By contrast, path entry finders are in a sense an implementation detailof the path based finder, and in fact, if the path based finder were to beremoved fromsys.meta_path, none of the path entry finder semanticswould be invoked.

5.5.1.Path entry finders

Thepath based finder is responsible for finding and loadingPython modules and packages whose location is specified with a stringpath entry. Most path entries name locations in the file system,but they need not be limited to this.

As a meta path finder, thepath based finder implements thefind_spec() protocol previouslydescribed, however it exposes additional hooks that can be used tocustomize how modules are found and loaded from theimport path.

Three variables are used by thepath based finder,sys.path,sys.path_hooks andsys.path_importer_cache. The__path__attributes on package objects are also used. These provide additional waysthat the import machinery can be customized.

sys.path contains a list of strings providing search locations formodules and packages. It is initialized from thePYTHONPATHenvironment variable and various other installation- andimplementation-specific defaults. Entries insys.path can namedirectories on the file system, zip files, and potentially other "locations"(see thesite module) that should be searched for modules, such asURLs, or database queries. Only strings should be present onsys.path; all other data types are ignored.

Thepath based finder is ameta path finder, so the importmachinery begins theimport path search by calling the pathbased finder'sfind_spec() method asdescribed previously. When thepath argument tofind_spec() is given, it will be alist of string paths to traverse - typically a package's__path__attribute for an import within that package. If thepath argument isNone, this indicates a top level import andsys.path is used.

The path based finder iterates over every entry in the search path, andfor each of these, looks for an appropriatepath entry finder(PathEntryFinder) for thepath entry. Because this can be an expensive operation (e.g. there may bestat() call overheads for this search), the path based finder maintainsa cache mapping path entries to path entry finders. This cache is maintainedinsys.path_importer_cache (despite the name, this cache actuallystores finder objects rather than being limited toimporter objects).In this way, the expensive search for a particularpath entrylocation'spath entry finder need only be done once. User code isfree to remove cache entries fromsys.path_importer_cache forcingthe path based finder to perform the path entry search again.

If the path entry is not present in the cache, the path based finder iteratesover every callable insys.path_hooks. Each of thepath entryhooks in this list is called with a single argument, thepath entry to be searched. This callable may either return apathentry finder that can handle the path entry, or it may raiseImportError. AnImportError is used by the path based finder tosignal that the hook cannot find apath entry finderfor thatpath entry. Theexception is ignored andimport path iteration continues. The hookshould expect either a string or bytes object; the encoding of bytes objectsis up to the hook (e.g. it may be a file system encoding, UTF-8, or somethingelse), and if the hook cannot decode the argument, it should raiseImportError.

Ifsys.path_hooks iteration ends with nopath entry finderbeing returned, then the path based finder'sfind_spec() method will storeNoneinsys.path_importer_cache (to indicate that there is no finder forthis path entry) and returnNone, indicating that thismeta path finder could not find the module.

If apath entry finderis returned by one of thepath entryhook callables onsys.path_hooks, then the following protocol is usedto ask the finder for a module spec, which is then used when loading themodule.

The current working directory -- denoted by an empty string -- is handledslightly differently from other entries onsys.path. First, if thecurrent working directory is found to not exist, no value is stored insys.path_importer_cache. Second, the value for the current workingdirectory is looked up fresh for each module lookup. Third, the path used forsys.path_importer_cache and returned byimportlib.machinery.PathFinder.find_spec() will be the actual currentworking directory and not the empty string.

5.5.2.Path entry finder protocol

In order to support imports of modules and initialized packages and also tocontribute portions to namespace packages, path entry finders must implementthefind_spec() method.

find_spec() takes two arguments: thefully qualified name of the module being imported, and the (optional) targetmodule.find_spec() returns a fully populated spec for the module.This spec will always have "loader" set (with one exception).

To indicate to the import machinery that the spec represents a namespaceportion, the path entry finder setssubmodule_search_locations toa list containing the portion.

在 3.4 版的變更:find_spec() replacedfind_loader() andfind_module(), both of whichare now deprecated, but will be used iffind_spec() is not defined.

Older path entry finders may implement one of these two deprecated methodsinstead offind_spec(). The methods are still respected for thesake of backward compatibility. However, iffind_spec() isimplemented on the path entry finder, the legacy methods are ignored.

find_loader() takes one argument, thefully qualified name of the module being imported.find_loader()returns a 2-tuple where the first item is the loader and the second itemis a namespaceportion.

For backwards compatibility with other implementations of the importprotocol, many path entry finders also support the same,traditionalfind_module() method that meta path finders support.However path entry finderfind_module() methods are never calledwith apath argument (they are expected to record the appropriatepath information from the initial call to the path hook).

Thefind_module() method on path entry finders is deprecated,as it does not allow the path entry finder to contribute portions tonamespace packages. If bothfind_loader() andfind_module()exist on a path entry finder, the import system will always callfind_loader() in preference tofind_module().

在 3.10 版的變更:Calls tofind_module() andfind_loader() by the importsystem will raiseImportWarning.

在 3.12 版的變更:find_module() andfind_loader() have been removed.

5.6.Replacing the standard import system

The most reliable mechanism for replacing the entire import system is todelete the default contents ofsys.meta_path, replacing thementirely with a custom meta path hook.

If it is acceptable to only alter the behaviour of import statementswithout affecting other APIs that access the import system, then replacingthe builtin__import__() function may be sufficient. This techniquemay also be employed at the module level to only alter the behaviour ofimport statements within that module.

To selectively prevent the import of some modules from a hook early on themeta path (rather than disabling the standard import system entirely),it is sufficient to raiseModuleNotFoundError directly fromfind_spec() instead of returningNone. The latter indicates that the meta path search should continue,while raising an exception terminates it immediately.

5.7.Package Relative Imports

Relative imports use leading dots. A single leading dot indicates a relativeimport, starting with the current package. Two or more leading dots indicate arelative import to the parent(s) of the current package, one level per dotafter the first. For example, given the following package layout:

package/__init__.pysubpackage1/__init__.pymoduleX.pymoduleY.pysubpackage2/__init__.pymoduleZ.pymoduleA.py

In eithersubpackage1/moduleX.py orsubpackage1/__init__.py,the following are valid relative imports:

from.moduleYimportspamfrom.moduleYimportspamashamfrom.importmoduleYfrom..subpackage1importmoduleYfrom..subpackage2.moduleZimporteggsfrom..moduleAimportfoo

Absolute imports may use either theimport<> orfrom<>import<>syntax, but relative imports may only use the second form; the reasonfor this is that:

importXXX.YYY.ZZZ

should exposeXXX.YYY.ZZZ as a usable expression, but .moduleY isnot a valid expression.

5.8.Special considerations for __main__

The__main__ module is a special case relative to Python's importsystem. As notedelsewhere, the__main__ moduleis directly initialized at interpreter startup, much likesys andbuiltins. However, unlike those two, it doesn't strictlyqualify as a built-in module. This is because the manner in which__main__ is initialized depends on the flags and other options withwhich the interpreter is invoked.

5.8.1.__main__.__spec__

Depending on how__main__ is initialized,__main__.__spec__gets set appropriately or toNone.

When Python is started with the-m option,__spec__ is setto the module spec of the corresponding module or package.__spec__ isalso populated when the__main__ module is loaded as part of executing adirectory, zipfile or othersys.path entry.

Inthe remaining cases__main__.__spec__ is set toNone, as the code used to populate the__main__ does not correspond directly with an importable module:

  • interactive prompt

  • -c 選項

  • running from stdin

  • running directly from a source or bytecode file

Note that__main__.__spec__ is alwaysNone in the last case,even if the file could technically be imported directly as a moduleinstead. Use the-m switch if valid module metadata is desiredin__main__.

Note also that even when__main__ corresponds with an importable moduleand__main__.__spec__ is set accordingly, they're still considereddistinct modules. This is due to the fact that blocks guarded byif__name__=="__main__": checks only execute when the module is usedto populate the__main__ namespace, and not during normal import.

5.9.References

The import machinery has evolved considerably since Python's early days. Theoriginalspecification for packages is still available to read,although some details have changed since the writing of that document.

The original specification forsys.meta_path wasPEP 302, withsubsequent extension inPEP 420.

PEP 420 introducednamespace packages forPython 3.3.PEP 420 also introduced thefind_loader() protocol as analternative tofind_module().

PEP 366 describes the addition of the__package__ attribute forexplicit relative imports in main modules.

PEP 328 introduced absolute and explicit relative imports and initiallyproposed__name__ for semanticsPEP 366 would eventually specify for__package__.

PEP 338 defines executing modules as scripts.

PEP 451 adds the encapsulation of per-module import state in specobjects. It also off-loads most of the boilerplate responsibilities ofloaders back onto the import machinery. These changes allow thedeprecation of several APIs in the import system and also addition of newmethods to finders and loaders.

註解

[1]

參閱types.ModuleType

[2]

The importlib implementation avoids using the return valuedirectly. Instead, it gets the module object by looking the module name upinsys.modules. The indirect effect of this is that an importedmodule may replace itself insys.modules. This isimplementation-specific behavior that is not guaranteed to work in otherPython implementations.