gpt4 book ai didi

python - setuptools 与 distutils : why is distutils still a thing?

转载 作者:IT老高 更新时间:2023-10-28 12:33:12 25 4
gpt4 key购买 nike

Python 在可用于打包和描述项目的工具方面有着令人困惑的历史:这些工具包括标准库中的 distutilsdistributedistutils2setuptools(可能还有更多)。 distributedistutils2 似乎已停止使用,取而代之的是 setuptools,这留下了两个相互竞争的标准。

据我了解,setuptools 提供了比 distutils 更多的选项(例如声明依赖项、测试等),但它并未包含在 Python 标准库中(目前?)。

Python 打包用户指南[ 1 ] 现在推荐:

Use setuptools to define projects and create Source Distributions.

并解释:

Although you can use pure distutils for many projects, it does not support defining dependencies on other projects and is missing several convenience utilities for automatically populating package metadata correctly that are provided by setuptools. Being outside the standard library, setuptools also offers a more consistent feature set across different versions of Python, and (unlike distutils), setuptools will be updated to produce the upcoming “Metadata 2.0” standard formats on all supported versions.

Even for projects that do choose to use distutils, when pip installs such projects directly from source (rather than installing from a prebuilt wheel file), it will actually build your project using setuptools instead.

但是,查看各种项目的 setup.py 文件发现这似乎不是一个实际的标准。许多软件包仍然使用 distutils 并且那些支持 setuptools 的软件包经常将 setuptoolsdistutils 混合使用,例如通过后备导入:

try:
from setuptools import setup
except ImportError:
from distutils.core import setup

随后试图找到一种方法来编写可以由 setuptoolsdistutils 安装的安装程序。这通常包括各种容易出错的依赖项检查方法,因为 distutils 不支持 setup 函数中的依赖项。

为什么人们仍在努力支持 distutils - setuptools 不在标准库中是唯一原因吗? distutils 的优点是什么,编写仅支持 setuptoolssetup.py 文件有什么缺点。

最佳答案

看看这个 SO 问题。它很好地解释了所有的包装方法,并且可能在一定程度上帮助回答您的问题:Differences between distribute, distutils, setuptools and distutils2?

Distutils is still the standard tool for packaging in Python. It is included in the standard library (Python 2 and Python 3.0 to 3.3). It is useful for simple Python distributions, but lacks features. It introduces the distutils Python package that can be imported in your setup.py script.

Setuptools was developed to overcome Distutils' limitations, and is not included in the standard library. It introduced a command-line utility called easy_install. It also introduced the setuptools Python package that can be imported in your setup.py script, and the pkg_resources Python package that can be imported in your code to locate data files installed with a distribution. One of its gotchas is that it monkey-patches the distutils Python package. It should work well with pip. The latest version was released in July 2013.

所以,正如您所见,setuptools 应该比 distutils 更受欢迎,我知道您的问题来自哪里,但是我认为 distutils 不会很快失去支持,因为简单地说,它在许多情况下与一些流行的遗留程序。而且您可能知道在遗留程序中更改这些类型的东西可能会很痛苦,并且会带来很多问题,例如不兼容,这将导致开发人员不得不重写源代码。所以就是这样,而且 distutils 是标准 python 库的一部分,而 setuptools 不是。因此,如果您正在创建一个 Python 程序,那么在这个时代,请使用 setuptools,但请记住,如果没有 distutils,setuptools 将永远存在。

关于python - setuptools 与 distutils : why is distutils still a thing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25337706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com