importlib
— 实现为
import
¶
Added in version 3.1.
源代码: Lib/importlib/__init__.py
介绍 ¶
目地对于
importlib
package is three-fold.
One is to provide the implementation of the
import
statement (and thus, by extension, the
__import__()
function) in Python source code. This provides an implementation of
import
which is portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.
Two, the components to implement
import
are exposed in this package, making it easier for users to create their own custom objects (known generically as an
importer
) to participate in the import process.
Three, the package contains modules exposing additional functionality for managing aspects of Python packages:
-
importlib.metadatapresents access to metadata from third-party distributions. -
importlib.resourcesprovides routines for accessing non-code “resources” from Python packages.
另请参阅
- 导入语句
-
The language reference for the
import语句。 - 包规范
-
Original specification of packages. Some semantics have changed since the writing of this document (e.g. redirecting based on
Noneinsys.modules). -
The
__import__()function -
The
import语句是用于此函数的句法糖。 - sys.path 模块搜索路径的初始化
-
The initialization of
sys.path. - PEP 235
-
在不区分大小写的平台导入
- PEP 263
-
定义 Python 源代码编码
- PEP 302
-
新的导入挂钩
- PEP 328
-
Imports: Multi-Line and Absolute/Relative
- PEP 366
-
Main module explicit relative imports
- PEP 420
-
隐式名称空间包
- PEP 451
-
A ModuleSpec Type for the Import System
- PEP 488
-
Elimination of PYO files
- PEP 489
-
Multi-phase extension module initialization
- PEP 552
-
Deterministic pycs
- PEP 3120
-
使用 UTF-8 作为默认源编码
- PEP 3147
-
PYC 存储库目录
函数 ¶
- importlib. __import__ ( 名称 , globals = None , locals = None , fromlist = () , level = 0 ) ¶
-
实现内置
__import__()函数。注意
以编程方式导入模块应该使用
import_module()而不是此函数。
- importlib. import_module ( 名称 , 包 = None ) ¶
-
导入模块。 name 自变量按绝对 (或相对) 术语方式指定要导入什么模块 (如
pkg.modor..mod)。若名称以相对术语方式指定,那么 包 自变量必须被设为充当解析包名的锚点包的名称 (如import_module('..mod', 'pkg.subpkg')将导入pkg.mod).The
import_module()函数充当简化包裹器围绕importlib.__import__()。这意味着函数的所有语义均派生自importlib.__import__()。这 2 函数的最重要区别是import_module()返回指定包或模块 (如pkg.mod),而__import__()返回顶层包或模块 (如pkg).若要动态导入从解释器开始执行起创建的模块 (如:创建 Python 源文件),可能需要调用
invalidate_caches()为使新模块能被导入系统注意到。3.3 版改变: 自动导入父级包。