python - 为什么 `pip3 install numpy` 比在 `install_requires` 中设置它快得多?

标签 python python-3.x numpy pip setuptools

以下内容发生在 Python 3 虚拟环境中。

我刚刚编写了一个需要numpy的小包。因此,在 setup.py 中,我编写了 install_requires=['numpy']。我运行了 python3 setup.py install,大约花了两分钟——我得到了通常在 numpy 安装中附带的日志、警告和配置的全屏转储。

然后,我创建了一个新的虚拟环境,这次只需编写pip3 install numpy——只花了几秒钟——然后运行python3 setup.py install code>,我几乎立刻就完成了。

两者之间有什么区别,为什么 pip3 install numpy 速度这么快?我是否应该包含一个 requirements.txt 以便人们可以 pip 安装需求而不是使用 setuptools?


请注意,当我编写 pip3 install numpy 时,我得到了以下内容:

Collecting numpy
  Using cached numpy-1.12.0-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy
Successfully installed numpy-1.12.0

是否有可能因为 numpy 轮已经缓存了,所以速度快得多?

最佳答案

pip install 使用 wheel 软件包,其设计部分目的是为了加快安装过程。

Rationale PEP 427 部分,它引入了 wheel 格式,指出:

Python needs a package format that is easier to install than sdist. Python's sdist packages are defined by and require the distutils and setuptools build systems, running arbitrary code to build-and-install, and re-compile, code just so it can be installed into a new virtualenv. This system of conflating build-install is slow, hard to maintain, and hinders innovation in both build systems and installers.

Wheel attempts to remedy these problems by providing a simpler interface between the build system and the installer. The wheel binary package format frees installers from having to know about the build system, saves time by amortizing compile time over many installations, and removes the need to install a build system in the target environment.

轮子安装速度更快,因为它是 Built Distribution format :

Built Distribution

A Distribution format containing files and metadata that only need to be moved to the correct location on the target system, to be installed. Wheel is such a format, whereas distutil’s Source Distribution is not, in that it requires a build step before it can be installed. This format does not imply that python files have to be precompiled (Wheel intentionally does not include compiled python files).

由于 numpy 的源代码发行版包含大量 C 代码,因此编译它需要相当长的时间,您在通过裸 setuptools 安装它时观察到了这一 pip 。 pip 避免了 C 代码的编译,因为轮子带有二进制代码(已经为您的系统编译)。

关于python - 为什么 `pip3 install numpy` 比在 `install_requires` 中设置它快得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42224325/

相关文章:

python - 如何使用 lxml 重置 etree

python - 如何计算 pandas DataFrame 中的 nan 值?

python - 查找最后一个非零元素 3D 数组 - numpy 数组

python - 为什么 pandas 在简单的数学运算上比 numpy 更快?

Python3.3 类型=文件名错误

Python-根据多个日期创建新列

python - 无法在 Fedora23 中安装 Pillow

python - PyCharm 报告未使用的局部变量,但此变量已在局部命名空间中使用

python - 确定数据框列中相同类型邻居范围的最快方法

python - 将 numpy 数组垂直添加到自身