史上最权威最详细的python中import机制

5. The import system

Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.import_module() and built-in __import__() can also be used to invoke the import machinery.

在一个module中的python代码通过import获得访问另一个module的代码的权限。import语句是唤起import机制最常用的方式,但不是唯一的方式。例如:函数importlib.import_module()和内置的__import__()也能用于唤起import机制。

The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope. The search operation of the import statement is defined as a call to the __import__() function, with the appropriate arguments. The return value of __import__() is used to perform the name binding operation of the import statement. See the import statement for the exact details of that name binding operation.

import语句结合两个操作;搜索已命名的module,然后把搜索的结果与本地域中的名字绑定。import语句定义为__import__()函数的一次调用。__import__()函数的返回值用于执行import语句的名字绑定操作。

A direct call to __import__() performs only the module search and, if found, the module creation operation. While certain side-effects may occur, such as the importing of parent packages, and the updating of various caches (including sys.modules), only the import statement performs a name binding operation.

直接调用__import__()仅仅执行module的搜索,如果找到,就会创建一个module对象。尽管可能有一些副作用,例如父包的导入、更新不容的缓存(包括sys。modules),但是只有import语句执行一次名字绑定操作。

When an import statement is executed, the standard builtin __import__() function is called. Other mechanisms for invoking the import system (such as importlib.import_module()) may choose to bypass __import__() and use their own solutions to implement import semantics.

当一条import语句被执行时,标准内置的__import__()函数被调用。其他的唤起import机制(例如:importlib.import_module())可能会选择绕过__import__()使用它们自己的解决方案来实现导入语义。

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked. These strategies can be modified and extended by using various hooks described in the sections below.

当一个module第一次导入的时候,python会搜索这个module如果找到,它会创建一个module对象并且初始化它。如果不能找到,一个ModuleNotFoundError异常会抛出。当唤起import机制时,python实现不同策略来搜索已命名module。这些策略能通过不同的hooks方式被修改和拓展。

Changed in version 3.3: The import system has been updated to fully implement the second phase of PEP 302. There is no longer any implicit import machinery - the full import system is exposed through sys.meta_path. In addition, native namespace package support has been implemented (see PEP 420).

在版本3.3中已经改变:import系统已经更新可以全面实现PEP 302的第二个阶段。不在有任何隐士的import机制,通过sys.meta_path来揭露完全的import系统。此外,已经实现了namespace package。

5.1. importlib

The importlib module provides a rich API for interacting with the import system. For example importlib.import_module() provides a recommended, simpler API than built-in __import__() for invoking the import machinery. Refer to the importlib library documentation for additional detail.

importlib 提供了丰富的与import系统交互的API。例如:importlib.import_module() 提供了一个推荐的、比内置__import__()更简单的唤起import机制的API。详情见importlib 库文档。

5.2. Packages

Python has only one type of module object, and all modules are of this type, regardless of whether the module is implemented in Python, C, or something else. To help organize modules and provide a naming hierarchy, Python has a concept of packages.

python只有一种module对象类型。为了帮助组织module并且提供名字机制,python有package的概念。

You can think of packages as the directories on a file system and modules as files within directories, but don’t take this analogy too literally since packages and modules need not originate from the file system. For the purposes of this documentation, we’ll use this convenient analogy of directories and files. Like file system directories, packages are organized hierarchically, and packages may themselves contain subpackages, as well as regular modules.

你可以把包看作是文件系统中的目录,把模块看作是目录中的文件,但不要把这个比喻看得太重,因为包和模块不一定来自于文件系统。在本文档中,我们将使用这个方便的目录和文件的类比。像文件系统的目录一样,包是分层组织的,包本身可能包含子包,以及常规模块。

It’s important to keep in mind that all packages are modules, but not all modules are packages. Or put another way, packages are just a special kind of module. Specifically, any module that contains a __path__ attribute is considered a package.

重要的是要记住,所有的包都是模块,但不是所有的模块都是包。或者换一种说法,包只是模块的一种特殊类型。具体来说,任何包含 __path__ 属性的模块都被认为是一个包。

All modules have a name. Subpackage names are separated from their parent package name by a dot, akin to Python’s standard attribute access syntax. Thus you might have a module called sys and a package called email, which in turn has a subpackage called email.mime and a module within that subpackage called email.mime.text.

所有模块都有一个名字。子包的名字与它们的父包的名字用一个点分开,类似于 Python 的标准属性访问语法。因此,你可能有一个叫 sys 的模块和一个叫 email 的包,而这个包又有一个叫 email.mime 的子包和这个子包中的一个叫 email.mime.text 的模块。

5.2.1. Regular packages   常规包

Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an __init__.py file. When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The __init__.py file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.

Python 定义了两种类型的包,常规包和命名空间包。常规包是传统的包,因为它们存在于 Python 3.2 和更早的版本中。一个常规包通常被实现为一个包含 __init__.py 文件的目录。当一个常规包被导入时,这个 __init__.py 文件被隐式执行,它所定义的对象被绑定到包的名字空间中。__init__.py 文件可以包含和其他模块一样的 Python 代码,当模块被导入时,Python 会给它添加一些额外的属性。

For example, the following file system layout defines a top level parent package with three subpackages:

例如,下面的文件系统布局定

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值