python - Python 在搜索非内置模块之前是否先搜索内置模块?

标签 python python-3.x

具体例子:

/tmp/test 中,我创建了一个包含以下内容的文件 itertools.py:

def f():
    print("it is my own itertools.")

我知道有一个名为 itertools 的内置模块

然后我从 /tmp/test 运行 python3

>>> import itertools
>>> itertools.__dict__
{'_tee': <class 'itertools._tee'>, 'groupby': <class 'itertools.groupby'>, ...}

所以我导入了内置模块 itertools 而不是 /tmp/test/itertools.py

似乎 Python 在搜索非内置模块之前先搜索内置模块。 这与 Python modules with identical names (i.e., reusing standard module names in packages) 相反.为什么?


一般规则:

Python 简而言之

When a module is loaded, __import__ first checks whether the module is built-in. The tuple sys.builtin_module_names names all built-in modules, but rebinding that tuple does not affect module loading.
The search for built-in modules also looks for modules in platform-specific loca‐ tions, such as the Registry in Windows.

If module M is not built-in, __import__ looks for M ’s code as a file on the filesystem. __import__ looks at the strings, which are the items of list sys.path , in order.

来自学习Python

In many cases, you can rely on the automatic nature of the module import search path and won’t need to configure this path at all. If you want to be able to import user- defined files across directory boundaries, though, you will need to know how the search path works in order to customize it. Roughly, Python’s module search path is composed of the concatenation of these major components, some of which are preset for you and some of which you can tailor to tell Python where to look:

  1. The home directory of the program
  2. PYTHONPATH directories (if set)
  3. Standard library directories
  4. The contents of any .pth files (if present)
  5. The site-packages home of third-party extensions

sys.path中是否存放了《Learning Python》中的五个地方?

Learning Python 的五个地方是在sys.builtin_module_names 中找不到内置模块才搜索的吗?

“3.标准库目录”不包括内置模块吗?内置模块存储在哪里? “3.标准库目录”和内置模块有什么关系?

谢谢。

最佳答案

这只是部分答案,但它可能有助于理清一些概念。

  • 内置模块通常用 C(至少对于 CPython)实现并编译。这些列在 sys.builtin_module_names 中。此类模块的示例是 sysitertools_collections(注意前导下划线)。

  • 然后是标准库模块。这些是普通的 Python 文件,在 Windows 上位于 Python 安装内的文件夹 lib 中。例如 collections(没有下划线)或 copy...

  • 然后是安装的扩展模块。这些可以是编译模块、普通 Python 文件等。在 Windows 上,可以在 lib 文件夹内的 site_packages 文件夹中找到它们。

如果我查看我的 sys.path:

['',                                           # current directory
 '...\\Python\\python35.zip',                  # no idea
 '...\\Python\\DLLs',                          # compiled modules (not builtins)
 '...\\Python\\lib',                           # standard library modules
 '...\\Python',                                # Python installation folder
 '...\\Python\\lib\\site-packages',            # installed modules
 ...]

似乎 1、3 和 5 包含在我的 sys.path 中,所以 可能 2 和 4(如果已设置)也将包含在其中。但这也可能是特定于 Windows 的。

关于你的标题问题:

Does Python search for a builtin module before searching for a nonbuiltin module?

是的!首先搜索内置函数,然后在当前目录或标准库(或已安装的模块)中查找模块。

例如,如果您的当前工​​作目录中有一个 sys.py 和一个 copy.py 文件,您尝试:

>>> import sys   # a builtin module
>>> sys
<module 'sys' (built-in)>     ... not the one from the current working directory

>>> import copy  # a standard library module
>>> copy.__file__
... current working directory ... not the one from the standard library

如果您真的对细节感兴趣,那么咨询 importlib 可能是个好主意。文档和引用的 PEP。就个人而言,我不会仅仅出于好奇而走那条路。

关于python - Python 在搜索非内置模块之前是否先搜索内置模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46133642/

相关文章:

python - Sphinx 将 NumPy 模块的文档添加到我的文档中

python - 使用 Cython distutils 时如何删除文档字符串?

python-3.x - 我如何从 Pandas 的时间戳中提取星期几

Python 3.5 "ImportError: cannot import name ' 某个名字'

python - 如何查找列表中嵌套列表的数量?

python - 高斯滤波opencv_python报错

python - FFMPEG 仅在写入管道时引发异常

python - 用 python 生成/合成声音?

python - 如何根据条件在 Pandas 中构建新列(新列应输出字符串)

python - Discord Python 机器人 : Levels and EXP System