setuptools vs.distutils:为什么distutils仍然是一个东西?

2024-05-16 18:46:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

据我所知,setuptoolsdistutils提供了更多的选项(例如声明依赖项、测试等),但是它还没有包含在python标准库中(还没有?)。

《Python打包用户指南》现在建议:

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不支持安装函数中的依赖项。

为什么人们还在为支持distutils而付出额外的努力setuptools不在标准库中是唯一的原因吗?distutils的优点是什么?编写只支持setuptoolssetup.py文件有什么缺点吗。


Tags: and工具to项目fromfor标准use
3条回答

看看这个问题。它很好地解释了所有的打包方法,在某种程度上可能有助于回答您的问题: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将永远不存在。

is the fact that setuptools is not in the standard library the only reason

这是一个原因。以下是直接从NumPy ^{}开始的:

if len(sys.argv) >= 2 and ('--help' in sys.argv[1:] or
        sys.argv[1] in ('--help-commands', 'egg_info', '--version',
                        'clean')):
    # Use setuptools for these commands (they don't work well or at all
    # with distutils).  For normal builds use distutils.
    try:
        from setuptools import setup
    except ImportError:
        from distutils.core import setup

所以如果能找到的话,NumPy更喜欢setuptools。但后来SciPy就这样做了,直到在某些情况下,它更喜欢distutils。引用提交日志:

Setuptools sets mode +x on the test scripts, so that Nose refuses to run
them. Better not do that.

当然,介于setuptoolsdistribute之间的merger应该在适当的时候解决所有这些问题,但是许多包仍然需要支持Python 2.6安装。

尽管setuptools无疑是更好的工具集,但我们仍然讨论和使用distutils有几个原因。

首先,distutils无处不在。如果您希望构建一个模块以便与其他人共享,并且没有任何复杂的需求,那么它将保证在您的工作机器上可用。如果必须支持较旧版本的python,或者发现自己在一个不熟悉的环境中工作,那么这一点尤其重要。

其次,setuptools提供了对distutils的增强。因此,它是根据distutils工具集建模的,并从那里获取它的所有结构。setuptools的文档假定读者熟悉distutils,并且只记录它如何增强基本工具集。可以认为distutils定义了方言,setuptools增强了方言。

我个人对于新项目的方法是从假设我将使用distutils开始的。只有当项目发展到需要setuptools的特性时,我才会进行升级。setuptools是distutils的替换项,它是对my setup.py的单行更改。

相关问题 更多 >