NumPy 的 setup.py 导入了它试图安装的同一个包

NumPy's setup.py imports the same package it is trying to install

查看 NumPy's setup.py 我注意到在某些时候它包含 NumPy 的子模块 numpy.distutils.

中的内容
from numpy.distutils.core import numpy_cmdclass

我真的看不出这是如何工作的,因为 numpy.distutils - 我认为 - 应该是它试图安装的同一个包的一部分。如果一个包依赖于它自己来安装,那不就好了吗?这背后的工作原理是什么?为什么这样做有效?

我注意到它做了完全相同的事情,但是来自实际上是包一部分的文件。在第二种情况下,使用相对导入不是更好吗?

这只是 Python 的常规导入机制在起作用。 the module search path is the "directory containing the input script", here setup.py. Packages 中第一个目录的搜索方式与模块相同。

所以当setup.py is executed, it finds the numpy folder in its own directory. That folder contains a file named __init__.py which marks it as a regular package. The module loader then goes on to find the sub-package distutils (which is vendored in so that it isn't required to be installed already) and the module core.py.