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__.py
one/
__init__.py
two/
__init__.py
three/
__init__.py
引入 parent.one
將隱式執行 parent/__init__.py
和 parent/one/__init__.py
。隨後引入 parent.two
或 parent.three
將分別執行 parent/two/__init__.py
和 parent/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
將包含 foo
、foo.bar
和 foo.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. Loading¶
If and when a module spec is found, the import machinery will use it (and the loader it contains) when loading the module. Here is an approximation of what happens during the loading portion of import:
module = None
if spec.loader is not None and hasattr(spec.loader, 'create_module'):
# It is assumed 'exec_module' will also be defined on the loader.
module = spec.loader.create_module(spec)
if module is None:
module = ModuleType(spec.name)
# The import-related module attributes get set here:
_init_module_attrs(spec, module)
if spec.loader is None:
# unsupported
raise ImportError
if spec.origin is None and spec.submodule_search_locations is not None:
# namespace package
sys.modules[spec.name] = module
elif not hasattr(spec.loader, 'exec_module'):
module = spec.loader.load_module(spec.name)
else:
sys.modules[spec.name] = module
try:
spec.loader.exec_module(module)
except BaseException:
try:
del sys.modules[spec.name]
except KeyError:
pass
raise
return sys.modules[spec.name]
Note the following details:
If there is an existing module object with the given name in
sys.modules
, import will have already returned it.The module will exist in
sys.modules
before the loader executes the module code. This is crucial because the module code may (directly or indirectly) import itself; adding it tosys.modules
beforehand prevents unbounded recursion in the worst case and multiple loading in the best.If loading fails, the failing module -- and only the failing module -- gets removed from
sys.modules
. Any module already in thesys.modules
cache, and any module that was successfully loaded as a side-effect, must remain in the cache. This contrasts with reloading where even the failing module is left insys.modules
.After the module is created but before execution, the import machinery sets the import-related module attributes ("_init_module_attrs" in the pseudo-code example above), as summarized in a later section.
Module execution is the key moment of loading in which the module's namespace gets populated. Execution is entirely delegated to the loader, which gets to decide what gets populated and how.
The module created during loading and passed to exec_module() may not be the one returned at the end of import [2].
在 3.4 版的變更: The import system has taken over the boilerplate responsibilities of
loaders. These were previously performed by the
importlib.abc.Loader.load_module()
method.
5.4.1. Loaders¶
Module loaders provide the critical function of loading: module execution.
The import machinery calls the importlib.abc.Loader.exec_module()
method with a single argument, the module object to execute. Any value
returned from exec_module()
is ignored.
Loaders must satisfy the following requirements:
If the module is a Python module (as opposed to a built-in module or a dynamically loaded extension), the loader should execute the module's code in the module's global name space (
module.__dict__
).If the loader cannot execute the module, it should raise an
ImportError
, although any other exception raised duringexec_module()
will be propagated.
In many cases, the finder and loader can be the same object; in such cases the
find_spec()
method would just return a
spec with the loader set to self
.
Module loaders may opt in to creating the module object during loading
by implementing a create_module()
method.
It takes one argument, the module spec, and returns the new module object
to use during loading. create_module()
does not need to set any attributes
on the module object. If the method returns None
, the
import machinery will create the new module itself.
在 3.4 版被加入: The create_module()
method of loaders.
在 3.4 版的變更: The load_module()
method was replaced by
exec_module()
and the import
machinery assumed all the boilerplate responsibilities of loading.
For compatibility with existing loaders, the import machinery will use
the load_module()
method of loaders if it exists and the loader does
not also implement exec_module()
. However, load_module()
has been
deprecated and loaders should implement exec_module()
instead.
The load_module()
method must implement all the boilerplate loading
functionality described above in addition to executing the module. All
the same constraints apply, with some additional clarification:
If there is an existing module object with the given name in
sys.modules
, the loader must use that existing module. (Otherwise,importlib.reload()
will not work correctly.) If the named module does not exist insys.modules
, the loader must create a new module object and add it tosys.modules
.The module must exist in
sys.modules
before the loader executes the module code, to prevent unbounded recursion or multiple loading.If loading fails, the loader must remove any modules it has inserted into
sys.modules
, but it must remove only the failing module(s), and only if the loader itself has loaded the module(s) explicitly.
在 3.5 版的變更: A DeprecationWarning
is raised when exec_module()
is defined but
create_module()
is not.
在 3.6 版的變更: An ImportError
is raised when exec_module()
is defined but
create_module()
is not.
在 3.10 版的變更: Use of load_module()
will raise ImportWarning
.
5.4.2. 子模組¶
When a submodule is loaded using any mechanism (e.g. importlib
APIs, the
import
or import-from
statements, or built-in __import__()
) a
binding is placed in the parent module's namespace to the submodule object.
For example, if package spam
has a submodule foo
, after importing
spam.foo
, spam
will have an attribute foo
which is bound to the
submodule. Let's say you have the following directory structure:
spam/
__init__.py
foo.py
and spam/__init__.py
has the following line in it:
from .foo import Foo
then executing the following puts name bindings for foo
and Foo
in the
spam
module:
>>> import spam
>>> spam.foo
<module 'spam.foo' from '/tmp/imports/spam/foo.py'>
>>> spam.Foo
<class 'spam.foo.Foo'>
Given Python's familiar name binding rules this might seem surprising, but
it's actually a fundamental feature of the import system. The invariant
holding is that if you have sys.modules['spam']
and
sys.modules['spam.foo']
(as you would after the above import), the latter
must appear as the foo
attribute of the former.
5.4.3. Module spec¶
The import machinery uses a variety of information about each module during import, especially before loading. Most of the information is common to all modules. The purpose of a module's spec is to encapsulate this import-related information on a per-module basis.
Using a spec during import allows state to be transferred between import system components, e.g. between the finder that creates the module spec and the loader that executes it. Most importantly, it allows the import machinery to perform the boilerplate operations of loading, whereas without a module spec the loader had that responsibility.
The module's spec is exposed as the __spec__
attribute on a module object.
See ModuleSpec
for details on the contents of
the module spec.
在 3.4 版被加入.
5.4.5. module.__path__¶
By definition, if a module has a __path__
attribute, it is a package.
A package's __path__
attribute is used during imports of its subpackages.
Within the import machinery, it functions much the same as sys.path
,
i.e. providing a list of locations to search for modules during import.
However, __path__
is typically much more constrained than
sys.path
.
__path__
must be an iterable of strings, but it may be empty.
The same rules used for sys.path
also apply to a package's
__path__
, and sys.path_hooks
(described below) are
consulted 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 implemented
prior to PEP 420. With the adoption of PEP 420, namespace packages no
longer need to supply __init__.py
files containing only __path__
manipulation code; the import machinery automatically sets __path__
correctly for the namespace package.
5.4.6. Module reprs¶
By default, all modules have a usable repr, however depending on the attributes set above, and in the module's spec, you can more explicitly control the repr of module objects.
If the module has a spec (__spec__
), the import machinery will try
to generate a repr from it. If that fails or there is no spec, the import
system will craft a default repr using whatever information is available
on the module. It will try to use the module.__name__
,
module.__file__
, and module.__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 spec is 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 the module'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 of module_repr()
, having been deprecated since Python 3.4, was
removed in Python 3.12 and is no longer called during the resolution of a
module's repr.
5.4.7. Cached bytecode invalidation¶
Before Python loads cached bytecode from a .pyc
file, it checks whether the
cache is up-to-date with the source .py
file. By default, Python does this
by storing the source's last-modified timestamp and size in the cache file when
writing it. At runtime, the import system then validates the cache file by
checking the stored metadata in the cache file against the source's
metadata.
Python also supports "hash-based" cache files, which store a hash of the source
file'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 the
resulting hash with the hash in the cache file. If a checked hash-based cache
file is found to be invalid, Python regenerates it and writes a new checked
hash-based cache file. For unchecked hash-based .pyc
files, Python simply
assumes the cache file is valid if it exists. Hash-based .pyc
files
validation behavior may be overridden with the --check-hash-based-pycs
flag.
在 3.7 版的變更: Added hash-based .pyc
files. Previously, Python only supported
timestamp-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 the path based finder
(PathFinder
), searches an import path,
which contains a list of path entries. Each path
entry names a location to search for modules.
The path based finder itself doesn't know how to import anything. Instead, it traverses the individual path entries, associating each of them with a path entry finder that knows how to handle that particular kind of path.
The default set of path entry finders implement all the semantics for finding
modules on the file system, handling special file types such as Python source
code (.py
files), Python byte code (.pyc
files) and
shared libraries (e.g. .so
files). When supported by the zipimport
module in the standard library, the default path entry finders also handle
loading all of these file types (other than shared libraries) from zipfiles.
Path entries need not be limited to file system locations. They can refer to URLs, database queries, or any other location that can be specified as a string.
The path based finder provides additional hooks and protocols so that you can 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 hook that implements HTTP semantics to find modules on the web. This hook (a callable) would return a path entry finder supporting the protocol described below, which was then used to get a loader for the module from the web.
A word of warning: this section and the previous both use the term finder,
distinguishing between them by using the terms meta path finder and
path entry finder. These two types of finders are very similar,
support similar protocols, and function in similar ways during the import
process, but it's important to keep in mind that they are subtly different.
In particular, meta path finders operate at the beginning of the import
process, as keyed off the sys.meta_path
traversal.
By contrast, path entry finders are in a sense an implementation detail
of the path based finder, and in fact, if the path based finder were to be
removed from sys.meta_path
, none of the path entry finder semantics
would be invoked.
5.5.1. Path entry finders¶
The path based finder is responsible for finding and loading Python modules and packages whose location is specified with a string path entry. Most path entries name locations in the file system, but they need not be limited to this.
As a meta path finder, the path based finder implements the
find_spec()
protocol previously
described, however it exposes additional hooks that can be used to
customize how modules are found and loaded from the import path.
Three variables are used by the path based finder, sys.path
,
sys.path_hooks
and sys.path_importer_cache
. The __path__
attributes on package objects are also used. These provide additional ways
that the import machinery can be customized.
sys.path
contains a list of strings providing search locations for
modules and packages. It is initialized from the PYTHONPATH
environment variable and various other installation- and
implementation-specific defaults. Entries in sys.path
can name
directories on the file system, zip files, and potentially other "locations"
(see the site
module) that should be searched for modules, such as
URLs, or database queries. Only strings should be present on
sys.path
; all other data types are ignored.
The path based finder is a meta path finder, so the import
machinery begins the import path search by calling the path
based finder's find_spec()
method as
described previously. When the path
argument to
find_spec()
is given, it will be a
list of string paths to traverse - typically a package's __path__
attribute for an import within that package. If the path
argument is
None
, this indicates a top level import and sys.path
is used.
The path based finder iterates over every entry in the search path, and
for each of these, looks for an appropriate path entry finder
(PathEntryFinder
) for the
path entry. Because this can be an expensive operation (e.g. there may be
stat()
call overheads for this search), the path based finder maintains
a cache mapping path entries to path entry finders. This cache is maintained
in sys.path_importer_cache
(despite the name, this cache actually
stores finder objects rather than being limited to importer objects).
In this way, the expensive search for a particular path entry
location's path entry finder need only be done once. User code is
free to remove cache entries from sys.path_importer_cache
forcing
the path based finder to perform the path entry search again.
If the path entry is not present in the cache, the path based finder iterates
over every callable in sys.path_hooks
. Each of the path entry
hooks in this list is called with a single argument, the
path entry to be searched. This callable may either return a path
entry finder that can handle the path entry, or it may raise
ImportError
. An ImportError
is used by the path based finder to
signal that the hook cannot find a path entry finder
for that path entry. The
exception is ignored and import path iteration continues. The hook
should expect either a string or bytes object; the encoding of bytes objects
is up to the hook (e.g. it may be a file system encoding, UTF-8, or something
else), and if the hook cannot decode the argument, it should raise
ImportError
.
If sys.path_hooks
iteration ends with no path entry finder
being returned, then the path based finder's
find_spec()
method will store None
in sys.path_importer_cache
(to indicate that there is no finder for
this path entry) and return None
, indicating that this
meta path finder could not find the module.
If a path entry finder is returned by one of the path entry
hook callables on sys.path_hooks
, then the following protocol is used
to ask the finder for a module spec, which is then used when loading the
module.
The current working directory -- denoted by an empty string -- is handled
slightly differently from other entries on sys.path
. First, if the
current working directory is found to not exist, no value is stored in
sys.path_importer_cache
. Second, the value for the current working
directory is looked up fresh for each module lookup. Third, the path used for
sys.path_importer_cache
and returned by
importlib.machinery.PathFinder.find_spec()
will be the actual current
working 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 to
contribute portions to namespace packages, path entry finders must implement
the find_spec()
method.
find_spec()
takes two arguments: the
fully qualified name of the module being imported, and the (optional) target
module. 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 namespace
portion, the path entry finder sets submodule_search_locations
to
a list containing the portion.
在 3.4 版的變更: find_spec()
replaced
find_loader()
and
find_module()
, both of which
are now deprecated, but will be used if find_spec()
is not defined.
Older path entry finders may implement one of these two deprecated methods
instead of find_spec()
. The methods are still respected for the
sake of backward compatibility. However, if find_spec()
is
implemented on the path entry finder, the legacy methods are ignored.
find_loader()
takes one argument, the
fully qualified name of the module being imported. find_loader()
returns a 2-tuple where the first item is the loader and the second item
is a namespace portion.
For backwards compatibility with other implementations of the import
protocol, many path entry finders also support the same,
traditional find_module()
method that meta path finders support.
However path entry finder find_module()
methods are never called
with a path
argument (they are expected to record the appropriate
path information from the initial call to the path hook).
The find_module()
method on path entry finders is deprecated,
as it does not allow the path entry finder to contribute portions to
namespace packages. If both find_loader()
and find_module()
exist on a path entry finder, the import system will always call
find_loader()
in preference to find_module()
.
在 3.10 版的變更: Calls to find_module()
and
find_loader()
by the import
system will raise ImportWarning
.
在 3.12 版的變更: find_module()
and find_loader()
have been removed.
5.6. Replacing the standard import system¶
The most reliable mechanism for replacing the entire import system is to
delete the default contents of sys.meta_path
, replacing them
entirely with a custom meta path hook.
If it is acceptable to only alter the behaviour of import statements
without affecting other APIs that access the import system, then replacing
the builtin __import__()
function may be sufficient. This technique
may also be employed at the module level to only alter the behaviour of
import statements within that module.
To selectively prevent the import of some modules from a hook early on the
meta path (rather than disabling the standard import system entirely),
it is sufficient to raise ModuleNotFoundError
directly from
find_spec()
instead of returning
None
. 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 relative import, starting with the current package. Two or more leading dots indicate a relative import to the parent(s) of the current package, one level per dot after the first. For example, given the following package layout:
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py
moduleA.py
In either subpackage1/moduleX.py
or subpackage1/__init__.py
,
the following are valid relative imports:
from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
Absolute imports may use either the import <>
or from <> import <>
syntax, but relative imports may only use the second form; the reason
for this is that:
import XXX.YYY.ZZZ
should expose XXX.YYY.ZZZ
as a usable expression, but .moduleY is
not a valid expression.
5.8. Special considerations for __main__¶
The __main__
module is a special case relative to Python's import
system. As noted elsewhere, the __main__
module
is directly initialized at interpreter startup, much like sys
and
builtins
. However, unlike those two, it doesn't strictly
qualify as a built-in module. This is because the manner in which
__main__
is initialized depends on the flags and other options with
which the interpreter is invoked.
5.8.1. __main__.__spec__¶
Depending on how __main__
is initialized, __main__.__spec__
gets set appropriately or to None
.
When Python is started with the -m
option, __spec__
is set
to the module spec of the corresponding module or package. __spec__
is
also populated when the __main__
module is loaded as part of executing a
directory, zipfile or other sys.path
entry.
In the remaining cases
__main__.__spec__
is set to None
, 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 always None
in the last case,
even if the file could technically be imported directly as a module
instead. Use the -m
switch if valid module metadata is desired
in __main__
.
Note also that even when __main__
corresponds with an importable module
and __main__.__spec__
is set accordingly, they're still considered
distinct modules. This is due to the fact that blocks guarded by
if __name__ == "__main__":
checks only execute when the module is used
to populate the __main__
namespace, and not during normal import.
5.9. References¶
The import machinery has evolved considerably since Python's early days. The original specification for packages is still available to read, although some details have changed since the writing of that document.
The original specification for sys.meta_path
was PEP 302, with
subsequent extension in PEP 420.
PEP 420 introduced namespace packages for
Python 3.3. PEP 420 also introduced the find_loader()
protocol as an
alternative to find_module()
.
PEP 366 describes the addition of the __package__
attribute for
explicit relative imports in main modules.
PEP 328 introduced absolute and explicit relative imports and initially
proposed __name__
for semantics PEP 366 would eventually specify for
__package__
.
PEP 338 defines executing modules as scripts.
PEP 451 adds the encapsulation of per-module import state in spec objects. It also off-loads most of the boilerplate responsibilities of loaders back onto the import machinery. These changes allow the deprecation of several APIs in the import system and also addition of new methods to finders and loaders.
註解