关于 Python 的 import
好久以前就被 Python 的相对与绝对导入所困扰。去年粗浅探究后自以为完全理解,近来又因 sys.path[0] 和 os.getcwd() 的不一致而刷新了认知...
Python 官方文档 5. The import system — Python 3.10.5 documentation 当然是最好的学习指南,但全部看完对我来说还是有点难度。这里只选择一些要点讨论。
from import
import A、import A as B、from A import B 结构中,A 最小只能到 module。因此,只有使用 from import 结构才可以单独获取 module 里的属性。另外,相对引用必须使用 from import 结构。
from module import * 将导入 module 中的所有成员(有单双下划线前导的成员除外)。对于 package 可在 __init__.py 中定义 __all__ = ["module", "module", ...] 来手动控制的实际导入内容。
Package 与 __init__.py
Python 3.3 以后的 package 不再硬性需要 __init__.py,普通文件夹等同于 __init__.py 留空的 namespace package。(关于 regular package 和 namespace package 的区别,参见 5. The import system — Python 3.10.5 documentation)
__init__.py 的作用在于当我们直接导入一个 package 的时候,实际上是执行了 __init__.py。换句话说,直接导入一个 package 就是把它看做一个逻辑写在 __init__.py 里的 module。
需要注意的是,对于形如 A.B.C 的导入,A、A.B、A.B.C 对应的 __init__.py 都会被执行。也就是说,只要导入路径经过该 package,该 package 的 __init__.py 就会被执行。
Submodules
When a submodule is loaded using any mechanism (e.g.
importlibAPIs, theimportorimport-fromstatements, or built-in__import__()) a binding is placed in the parent module’s namespace to the submodule object. For example, if packagespamhas a submodulefoo, after importingspam.foo,spamwill have an attributefoowhich is bound to the submodule....
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']andsys.modules['spam.foo'](as you would after the above import), the latter must appear as thefooattribute of the former.
这是说,import 进来的 module 会被挂载到本 module 上作为其属性。
这个性质可以弄出来很多看上去很奇怪的玩意儿,比如说自己导入自己后可以 me.me.me.me... 无限嵌套之类的...
另外,对于形如 import A.B.C 的导入,A、A.B、A.B.C 都会被挂载到本 module 上。然而,from A.B import C 却只会挂载 C,而 import A.B.C as D 也只会挂载 D ,即使 A、A.B 都被执行且都在 sys.modules 里。
sys.path
A list of strings that specifies the search path for modules. Initialized from the environment variable
PYTHONPATH, plus an installation-dependent default.As initialized upon program startup, the first item of this list,
path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input),path[0]is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result ofPYTHONPATH.A program is free to modify this list for its own purposes. Only strings and bytes should be added to
sys.path; all other data types are ignored during import.— sys — System-specific parameters and functions — Python 3.10.5 documentation
sys.path 是 Python 搜索 module 的基准目录(即绝对导入)。其由环境变量 PYTHONPATH 和一些默认路径(和安装环境有关,参见 PYTHONHOME)组成,而在运行 script 时,script 的所在目录会被临时加入 sys.path[0] 中。如果运行的并不是 script(例如是交互式运行或从 stdin 中读取脚本代码),sys.path[0] 则被设置为空字符串,代表当前工作目录。
sys.path 有优先级,排在前面的优先级高。
需要特别注意的是,script 的所在目录不是当前工作目录。例如,在 D:\test 下执行
python path/to/file.py
时,sys.path[0] 为 D:\test\path\to\file.py,而当前工作目录则是 D:\test(也即 os.getcwd())。
当前工作目录是 Python 寻找其他文件时的基准路径,而所有绝对导入操作都只与 sys.path 有关,两者是完全不同的。
python -m 的情况稍有不同,参见后文。
python -m
Search
sys.pathfor the named module and execute its contents as the__main__module.Since the argument is a module name, you must not give a file extension (
.py). The module name should be a valid absolute Python module name, but the implementation may not always enforce this (e.g. it may allow you to use a name that includes a hyphen).Package names (including namespace packages) are also permitted. When a package name is supplied instead of a normal module, the interpreter will execute
<pkg>.__main__as the main module. This behaviour is deliberately similar to the handling of directories and zipfiles that are passed to the interpreter as the script argument.Note
This option cannot be used with built-in modules and extension modules written in C, since they do not have Python module files. However, it can still be used for precompiled modules, even if the original source file is not available.
If this option is given, the first element of
sys.argvwill be the full path to the module file (while the module file is being located, the
first element will be set to"-m"). As with the-coption, the current directory will be added to the start ofsys.path.
-Ioption can be used to run the script in isolated mode wheresys.pathcontains neither the current directory nor the user’s site-packages directory. AllPYTHON*environment variables are ignored, too.Many standard library modules contain code that is invoked on their execution as a script. An example is the
timeitmodule:python -m timeit -s 'setup here' 'benchmarked code here'
python -m timeit -h # for details
Raises an auditing event
cpython.run_modulewith argumentmodule-name.See also
Equivalent functionality directly available to Python code
PEP 338 – Executing modules as scripts
Changed in version 3.1: Supply the package name to run a
__main__submodule.Changed in version 3.4: namespace packages are also supported
— 1. Command line and environment — Python 3.10.5 documentation
在 sys.path 指定的目录中寻找 module 并以 __main__ module 的身份执行指定 module。
注意不要在名字后面加 .py,因为我们已经把执行的文件当作 module 来看待。
如果指定的是一个 Package name(即目录名),将会执行 <pkg>.__main__(即 <pkg>/__main__.py)。
另外,如果使用 python -m a.b.module,sys.argv 的首位将被设置为被执行 module 文件的完整路径(与之相对,python a/b/module.py 中 sys.argv[0] 将会是相对当前工作目录的路径,即 a/b/module.py);同时,当前工作目录会被加入 sys.path 的首位。
python -m A.B.module 将顺次执行 A、A.B 的 __init__.py,即使该 module 没有任何导入行为。
python -m 对于直接执行 package 内部的代码是必要的。若直接以 script 方式运行,一旦涉及到任何高于该 script 所在目录(含该目录)的相对导入,Python 就会抛出如下错误:
ImportError: attempted relative import with no known parent package
而一个 module 也不能导入超过 python -m 参数指定的最顶层结构的 module,否则会抛出错误:
ImportError: attempted relative import beyond top-level package
sys.modules
The first place checked during import search is
sys.modules. This mapping serves as a cache of all modules that have been previously imported, including the intermediate paths. So iffoo.bar.bazwas previously imported,sys.moduleswill contain entries forfoo,foo.bar, andfoo.bar.baz. Each key will have as its value the corresponding module object.During import, the module name is looked up in
sys.modulesand if present, the associated value is the module satisfying the import, and the process completes. However, if the value isNone, then aModuleNotFoundErroris raised. If the module name is missing, Python will continue searching for the module.
sys.modulesis writable. Deleting a key may not destroy the associated module (as other modules may hold references to it),
but it will invalidate the cache entry for the named module, causing Python to search anew for the named module upon its next
import. The key can also be assigned toNone, forcing the next import of the module to result in aModuleNotFoundError.Beware though, as if you keep a reference to the module object, invalidate its cache entry in
sys.modules, and then re-import the named module, the two module objects will not be the same. By contrast,importlib.reload()will reuse the same module object, and simply reinitialise the module contents by rerunning the module’s code.
sys.modules 是一个 dict,Python 在导入之前会去检查 sys.module 里是否已经存有需要的 module 的 module object。如果有,就直接用这个;如果值为 None(意思是以前找过但没找到),就直接报错;如果该键值对不存在,就继续搜索过程。总之,sys.modules 扮演了一个类似 cache 的角色。
对于形如 A.B.C 的导入,Python 会顺次导入 A、A.B 和 A.B.C 并把他们加入 sys.modules。
参考
关于 Python 的 import的更多相关文章
- python中import和from...import区别
在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中 ...
- Python中import的使用
python中的import语句是用来导入模块的,在python模块库中有着大量的模块可供使用,要想使用这些文件需要用import语句把指定模块导入到当前程序中. import语句的作用 import ...
- python 的import机制2
http://blog.csdn.net/sirodeng/article/details/17095591 python 的import机制,以备忘: python中,每个py文件被称之为模块, ...
- python之import机制
1. 标准 import Python 中所有加载到内存的模块都放在 sys.modules .当 import 一个模块时首先会在这个列表中查找是否已经加载了此模块,如果加载了则只是将 ...
- python中import和from...import...的区别
python中import和from...import...的区别: 只用import时,如import xx,引入的xx是模块名,而不是模块内具体的类.函数.变量等成员,使用该模块的成员时需写成xx ...
- python的import与from...import的不同之处
在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中 ...
- (原)python中import caffe提示no module named google.protobuf.internal
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...
- linux环境下 python环境import找不到自定义的模块
linux环境下 python环境import找不到自定义的模块 问题现象: Linux环境中自定义的模块swport,import swport 出错.swport模块在/root/sw/目录下. ...
- 关于Python的import机制原理
很多人用过python,不假思索地在脚本前面加上import module_name,但是关于import的原理和机制,恐怕没有多少人真正的理解.本文整理了Python的import机制,一方面自己总 ...
- [转] Python的import初探
转载自:http://www.lingcc.com/2011/12/15/11902/#sec-1 日常使用python编程时,为了用某个代码模块,通常需要在代码中先import相应的module.那 ...
随机推荐
- Bugku练习题---MISC---啊哒
Bugku练习题---MISC---啊哒 flag:flag{3XiF_iNf0rM@ti0n} 解题步骤: 1.观察题目,下载附件 2.下载以后发现是一张图片,从表面看没有什么有价值的信息 3.直接 ...
- XCTF练习题---MISC---坚持60S
XCTF练习题---MISC---坚持60S flag:flag{DajiDali_JinwanChiji} 解题步骤: 1.观察题目,下载附件,是一个java文件 2.打开玩了一会,真鸡儿难,试着反 ...
- 记一次调试YOLOv5+DeepSort车辆跟踪项目的经过
摘要:学习别人的开源项目是日常的一项必备技能,本文通过一个车辆跟踪(YOLOv5+DeepSort)的例子介绍如何配置和调试GitHub上的开源代码.以第一人称的视角给出本人调试代码的过程,包括项目r ...
- 深入HTTP请求流程
1.HTTP协议介绍 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,它是从WEB服务器传输超文本标记语言(HTML)到 ...
- 2021夏季学期华清大学EE数算OJ2:难缠的店长
2021年夏季学期华清大学电子系数算oj2题解 某知名oier锐评蒟蒻的oj1题解: 话不多说,进入oj2题解: 难缠的oj 之 难缠的店长 当时读完我已经因为无良甲方的行为出离愤怒了!但是做题还是要 ...
- 为什么 Redis 要有哨兵机制?
作者:小林coding 计算机八股文刷题网站:https://xiaolincoding.com 大家好,我是小林. 这次聊聊,Redis 的哨兵机制. 提纲 为什么要有哨兵机制? 在 Redis 的 ...
- TinyMCE简介
TinyMCE是一款开源.易用.UI时新的富文本编辑器. 插件丰富,自带插件基本满足要求 可扩展性强,可自定义功能 界面好看,符合现代审美 提供经典.内联.沉浸无干扰三种模式 官网:https://w ...
- 分布式下Session一致性问题
一.Session一致性问题 1.1 什么是Session 用户使用网站的服务,基本上需要浏览器和web服务器进行多次交互,web服务器如何知道哪些请求是来自哪个会话的? 具体方式为:在会话开始时,分 ...
- Java高并发-多线程基础
一.什么是线程 线程是进程内的执行单元. 二.线程的基本操作 2.1 状态与操作 2.2 新建线程 Thread t1 = new Thread(new CreateThread()); t1.sta ...
- [2021-TKK 暑期训练第一场] 1585:下馆子-3
题目做了超链接 参考官方题解,作部分优化 下馆子 -3 题意: 给定n组数据,由name,time构成 当只有一个最大值时,输出该同学 当不止有一个最大值时,输出最先大于等于max次的同学 题解: 考 ...