这种情况

我正在尝试将一个开源库移植到Python 3。(如果有人想知道的话,是SymPy。)

因此,在为Python 3构建时,我需要自动运行2to3。要做到这一点,我需要使用分布。因此,我需要移植当前系统,这(根据doctest)是distutils。

这个问题

不幸的是,我不确定这些模块之间的区别——distutils、distribute、setuptools。文档是粗略的,因为它们似乎都是彼此的分支,旨在在大多数情况下兼容(但实际上,不是所有情况)……等等。

这个问题

有人能解释一下其中的区别吗?我应该用什么?最现代的解决方案是什么?(作为题外话,我也希望有一些关于移植到分发版的指南,但这有点超出了问题的范围…)


当前回答

在2014年底更新了这个问题,幸运的是,Python打包的混乱已经被Continuum的“conda”包管理器大大清理了。

特别是,conda可以快速地创建conda“环境”。您可以使用不同版本的Python配置您的环境。例如:

Conda create -n py34 python=3.4蟒蛇

Conda create -n py26 python=2.6 anaconda

将使用不同版本的Python创建两个(“py34”或“py26”)Python环境。

之后,你可以使用特定版本的Python调用环境:

源激活<env名称>

当您需要处理不同版本的Python时,这个特性似乎特别有用。

此外,conda还具有以下特点:

Python不可知论者 跨平台的 不需要管理权限 智能依赖管理(通过SAT求解器) 很好地处理C, Fortran和系统级库,你可能不得不链接

如果你在科学计算领域,最后一点尤其重要。

其他回答

注意:回答已弃用,分发现已废弃。这个答案不再有效,因为Python打包权威机构已经成立,并做了大量工作来清理这个问题。


是的,你说对了。:-o我认为目前首选的软件包是Distribute,它是setuptools的一个分支,是distutils(原始打包系统)的扩展。Setuptools没有被维护,所以它被分叉和重命名,但是当安装时它使用Setuptools的包名!我认为大多数Python开发人员现在都使用分发,我可以肯定地说我是这样做的。

在2014年底更新了这个问题,幸运的是,Python打包的混乱已经被Continuum的“conda”包管理器大大清理了。

特别是,conda可以快速地创建conda“环境”。您可以使用不同版本的Python配置您的环境。例如:

Conda create -n py34 python=3.4蟒蛇

Conda create -n py26 python=2.6 anaconda

将使用不同版本的Python创建两个(“py34”或“py26”)Python环境。

之后,你可以使用特定版本的Python调用环境:

源激活<env名称>

当您需要处理不同版本的Python时,这个特性似乎特别有用。

此外,conda还具有以下特点:

Python不可知论者 跨平台的 不需要管理权限 智能依赖管理(通过SAT求解器) 很好地处理C, Fortran和系统级库,你可能不得不链接

如果你在科学计算领域,最后一点尤其重要。

我是distutills维护者和distutil2 /打包贡献者。我在ConFoo 2011上做了一个关于Python打包的演讲,这些天我正在写它的扩展版本。这本书还没有出版,所以这里有一些摘录,应该有助于定义一些东西。

Distutils is the standard tool used for packaging. It works rather well for simple needs, but is limited and not trivial to extend. Setuptools is a project born from the desire to fill missing distutils functionality and explore new directions. In some subcommunities, it’s a de facto standard. It uses monkey-patching and magic that is frowned upon by Python core developers. Distribute is a fork of Setuptools that was started by developers feeling that its development pace was too slow and that it was not possible to evolve it. Its development was considerably slowed when distutils2 was started by the same group. 2013-August update: distribute is merged back into setuptools and discontinued. Distutils2 is a new distutils library, started as a fork of the distutils codebase, with good ideas taken from setup tools (of which some were thoroughly discussed in PEPs), and a basic installer inspired by pip. The actual name you use to import Distutils2 is packaging in the Python 3.3+ standard library, or distutils2 in 2.4+ and 3.1–3.2. (A backport will be available soon.) Distutils2 did not make the Python 3.3 release, and it was put on hold.

更多信息:

Distutils的命运- Pycon峰会+包装Sprint详细报告 Distutils和distutil2之间的快速区别

我希望尽快完成我的指南,它将包含关于每个库的优点和缺点的更多信息以及过渡指南。

我意识到我已经回答了你的第二个问题,而没有解决你最初问题中毫无疑问的假设:

我正在尝试将一个开源库(SymPy,如果有人想知道的话)移植到Python 3。来 这样做,我需要在为Python 3构建时自动运行2to3。

你可以,但不需要。其他策略请参见http://docs.python.org/dev/howto/pyporting

要做到这一点,我需要使用分配,

distutils支持代码(不是文档字符串)的构建时2to3转换,其方式与distribute 's不同:http://docs.python.org/dev/howto/pyporting#during-installation

截至2022年5月,这个问题的大多数其他答案都已经过时好几年了。当你遇到关于Python打包问题的建议时,记得查看发布日期,不要相信过时的信息。

Python打包用户指南值得一读。每一页都有一个“最近更新”的日期显示,所以你可以检查手册的最近,它是相当全面的。事实上,它托管在Python软件基金会的python.org的子域上,这增加了它的可信度。项目摘要页面在这里尤其相关。

工具概述:

以下是对Python打包环境的总结:

支持工具:

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. It sees regular releases. Official docs | Pypi page | GitHub repo | setuptools section of Python Package User Guide scikit-build is an improved build system generator that internally uses CMake to build compiled Python extensions. Because scikit-build isn't based on distutils, it doesn't really have any of its limitations. When ninja-build is present, scikit-build can compile large projects over three times faster than the alternatives. It should work well with pip. Official docs | Pypi page | GitHub repo | scikit-build section of Python Package User Guide distlib is a library that provides functionality that is used by higher level tools like pip. Official Docs | Pypi page | Bitbucket repo | distlib section of Python Package User Guide packaging is also a library that provides functionality used by higher level tools like pip and setuptools Official Docs | Pypi page | GitHub repo | packaging section of Python Package User Guide

弃用/废弃的工具:

distutils is still included in the standard library of Python, but is considered deprecated as of Python 3.10. 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. Official docs | distutils section of Python Package User Guide distribute was a fork of setuptools. It shared the same namespace, so if you had Distribute installed, import setuptools would actually import the package distributed with Distribute. Distribute was merged back into Setuptools 0.7, so you don't need to use Distribute any more. In fact, the version on Pypi is just a compatibility layer that installs Setuptools. distutils2 was an attempt to take the best of distutils, setuptools and distribute and become the standard tool included in Python's standard library. The idea was that distutils2 would be distributed for old Python versions, and that distutils2 would be renamed to packaging for Python 3.3, which would include it in its standard library. These plans did not go as intended, however, and currently, distutils2 is an abandoned project. The latest release was in March 2012, and its Pypi home page has finally been updated to reflect its death.

其他:

还有其他工具,如果你感兴趣,请阅读Python打包用户指南中的项目摘要。为了不重复那一页,我不会全部列出,并使答案与问题相匹配,这个问题只涉及distribute、distutils、setuptools和distutils2。

推荐:

如果所有这些对您来说都是新的,并且您不知道从哪里开始,我建议您学习setuptools,以及pip和virtualenv,它们都可以很好地配合使用。

如果您正在研究virtualenv,您可能会对这个问题感兴趣:venv、pyvenv、pyenv、virtualenv、virtualenvwrapper等之间的区别是什么?(是的,我知道,我和你一起抱怨。)