distribution、distutils、setuptools 和 distutils2 之间的区别?
我正在尝试
将开源库移植到 Python 3。(SymPy,如果有人想知道的话。)
所以,我需要在构建 Python 3 时自动运行 2to3
。为此,我需要使用 distribute
。因此,我需要移植当前系统,(根据文档测试)是 distutils
。
问题
不幸的是,我不确定这些模块之间有什么区别 - distutils
、distribute
、setuptools
。该文档最好是粗略的,因为它们似乎都是彼此的分叉,旨在在大多数情况下兼容(但实际上不是全部)......等等,等等。
问题
有人可以解释一下这些差异吗?我应该使用什么?最现代的解决方案是什么? (顺便说一句,我还希望获得一些有关移植到 Distribute 的指南,但这有点超出了问题的范围......)
The Situation
I’m trying to port an open-source library to Python 3. (SymPy, if anyone is wondering.)
So, I need to run 2to3
automatically when building for Python 3. To do that, I need to use distribute
. Therefore, I need to port the current system, which (according to the doctest) is distutils
.
The Problem
Unfortunately, I’m not sure what’s the difference between these modules—distutils
, distribute
, setuptools
. The documentation is sketchy as best, as they all seem to be a fork of one another, intended to be compatible in most circumstances (but actually, not all)…and so on, and so forth.
The Question
Could someone explain the differences? What am I supposed to use? What is the most modern solution? (As an aside, I’d also appreciate some guide on porting to Distribute
, but that’s a tad beyond the scope of the question…)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
截至 2022 年 5 月,这个问题的大多数其他答案都已经过时了好几年。当您遇到有关 Python 打包问题的建议时,请记住查看发布日期,不要相信过时的信息。
Python 打包用户指南值得一读。每个页面都会显示“最后更新”日期,因此您可以检查手册的最新版本,而且内容相当全面。事实上,它托管在 Python 软件基金会的 python.org 子域上,这更增加了它的可信度。 项目摘要页面在这里特别相关。
工具摘要:
以下是 Python 打包情况的摘要:
支持的工具:
setuptools
是为了克服 Distutils 的限制而开发的,未包含在标准库中。它引入了一个名为easy_install
的命令行实用程序。它还介绍了可以在setup.py
脚本中导入的setuptools
Python 包,以及可以在您的setup.py
脚本中导入的pkg_resources
Python 包。用于查找随发行版一起安装的数据文件的代码。它的问题之一是它对distutils
Python 包进行了猴子修补。它应该可以与pip
很好地配合。 它会定期发布。setuptools
部分scikit-build
是一个改进的构建系统生成器,它在内部使用 CMake 来构建已编译的 Python 扩展。因为 scikit-build 不是基于 distutils,所以它实际上没有任何限制。当 ninja-build 存在时,scikit-build 编译大型项目的速度比替代方案快三倍以上。它应该与pip
配合良好。scikit-build
部分distlib
是一个库,提供由pip
等高级工具使用的功能。distlib
部分packaging
也是一个库,提供pip
和setuptools
等高级工具使用的功能
打包
部分已弃用/废弃的工具:
distutils
仍然包含在 Python 标准库中,但从 Python 3.10 开始被视为已弃用。它对于简单的 Python 发行版很有用,但缺乏功能。它引入了可以在setup.py
脚本中导入的distutils
Python 包。distutils
部分distribute
是setuptools
的一个分支。它共享相同的命名空间,因此如果您安装了 Distribute,import setuptools
实际上会导入随 Distribute 分发的包。 Distribute 已合并回 Setuptools 0.7,因此您不再需要使用 Distribute。其实Pypi上的版本只是安装Setuptools的兼容层。distutils2
试图充分利用distutils
、setuptools
和distribute 并成为Python标准库中包含的标准工具。我们的想法是,
distutils2
将针对旧 Python 版本进行分发,而distutils2
将在 Python 3.3 中重命名为packaging
,其中将包含它在其标准库中。然而,这些计划并没有按预期进行,目前,distutils2
是一个废弃的项目。最新版本发布于 2012 年 3 月,其 Pypi 主页终于更新以反映其已消亡的情况。其他:
还有其他工具,如果您有兴趣,请阅读Python中的Project Summaries包装用户指南。我不会将它们全部列出,以免重复该页面,并保持答案与问题匹配,该问题仅涉及
distribute
、distutils
、setuptools< /code> 和
distutils2
。建议:
如果所有这些对您来说都是新的,并且您不知道从哪里开始,我建议学习
setuptools
以及pip
和virtualenv
,它们都可以很好地协同工作。如果您正在研究
virtualenv
,您可能会对这个问题感兴趣:之间有什么区别>venv
、pyvenv
、pyenv
、virtualenv
、virtualenvwrapper
、等等?。 (是的,我知道,我和你一起呻吟。)As of May 2022, most of the other answers to this question are several years out-of-date. When you come across advice on Python packaging issues, remember to look at the date of publication, and don't trust out-of-date information.
The Python Packaging User Guide is worth a read. Every page has a "last updated" date displayed, so you can check the recency of the manual, and it's quite comprehensive. The fact that it's hosted on a subdomain of python.org of the Python Software Foundation just adds credence to it. The Project Summaries page is especially relevant here.
Summary of tools:
Here's a summary of the Python packaging landscape:
Supported tools:
setuptools
was developed to overcome Distutils' limitations, and is not included in the standard library. It introduced a command-line utility calledeasy_install
. It also introduced thesetuptools
Python package that can be imported in yoursetup.py
script, and thepkg_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 thedistutils
Python package. It should work well withpip
. It sees regular releases.setuptools
section of Python Package User Guidescikit-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 withpip
.scikit-build
section of Python Package User Guidedistlib
is a library that provides functionality that is used by higher level tools likepip
.distlib
section of Python Package User Guidepackaging
is also a library that provides functionality used by higher level tools likepip
andsetuptools
packaging
section of Python Package User GuideDeprecated/abandoned tools:
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 thedistutils
Python package that can be imported in yoursetup.py
script.distutils
section of Python Package User Guidedistribute
was a fork ofsetuptools
. 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 ofdistutils
,setuptools
anddistribute
and become the standard tool included in Python's standard library. The idea was thatdistutils2
would be distributed for old Python versions, and thatdistutils2
would be renamed topackaging
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.Others:
There are other tools, if you are interested, read Project Summaries in the Python Packaging User Guide. I won't list them all, to not repeat that page, and to keep the answer matching the question, which was only about
distribute
,distutils
,setuptools
anddistutils2
.Recommendation:
If all of this is new to you, and you don't know where to start, I would recommend learning
setuptools
, along withpip
andvirtualenv
, which all work very well together.If you're looking into
virtualenv
, you might be interested in this question: What is the difference betweenvenv
,pyvenv
,pyenv
,virtualenv
,virtualenvwrapper
, etc?. (Yes, I know, I groan with you.)我是 distutils 维护者和 distutils2/打包贡献者。我在 ConFoo 2011 上做了一个关于 Python 打包的演讲,这些天我正在编写它的扩展版本。它尚未发布,因此以下摘录应该有助于定义事物。
Distutils 是用于打包的标准工具。它可以很好地满足简单的需求,但受到限制并且扩展起来并不容易。
Setuptools 是一个出于填补 distutils 功能缺失和探索新方向的愿望而诞生的项目。在某些子社区中,它是事实上的标准。它使用了 Python 核心开发人员不赞成的猴子补丁和魔法。
Distribute 是Setuptools 的一个分支,开发人员认为它的开发速度太慢并且不可能发展它,因此开始了它的开发。当同一组启动 distutils2 时,它的开发速度显着减慢。 2013 年 8 月更新:distribute 合并回 setuptools 并停止使用。
Distutils2 是一个新的 distutils 库,最初是作为 distutils 代码库的一个分支,具有从安装工具中汲取的好想法(其中一些在 PEP 中进行了深入讨论),以及受启发的基本安装程序点。
用于导入 Distutils2 的实际名称是 Python 3.3+ 标准库中的Distutils2 没有发布 Python 3.3,它被搁置了。packaging
,或 2.4+ 和 3.1–3.2 中的distutils2
。 (向后移植很快就会推出。)更多信息:
我希望尽快完成我的指南,它将包含有关每个库的优点和缺点的更多信息以及过渡指南。
I’m a distutils maintainer and distutils2/packaging contributor. I did a talk about Python packaging at ConFoo 2011 and these days I’m writing an extended version of it. It’s not published yet, so here are excerpts that should help define things.
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 isDistutils2 did not make the Python 3.3 release, and it was put on hold.packaging
in the Python 3.3+ standard library, ordistutils2
in 2.4+ and 3.1–3.2. (A backport will be available soon.)More info:
I hope to finish my guide soon, it will contain more info about each library’s strong and weak points and a transition guide.
注意:答案已弃用,分发现已过时。自从 Python 包装管理局成立并做了很多清理工作以来,这个答案就不再有效了。
是的,你明白了。 :-o 我认为目前首选的软件包是 Distribute,它是 setuptools 的一个分支,它们是 distutils(原始打包系统)的扩展。 setuptools 没有得到维护,因此被分叉并重命名,但是安装时它使用 setuptools 的包名称!我认为大多数 Python 开发人员现在都使用 Distribute,我可以肯定地说我是这么做的。
NOTE: Answer deprecated, Distribute now obsolete. This answer is no longer valid since the Python Packaging Authority was formed and has done a lot of work cleaning this up.
Yep, you got it. :-o I think at this time the preferred package is Distribute, which is a fork of setuptools, which are an extension of distutils (the original packaging system). Setuptools was not being maintained so is was forked and renamed, however when installed it uses the package name of setuptools! I think most Python developers now use Distribute, and I can say for sure that I do.
我意识到我已经回答了你的第二个问题,但没有解决你原来问题中未经质疑的假设:
您可能,而不需要。其他策略在 http://docs.python.org/dev/howto/pyporting
您可能:) distutils 支持代码(而不是文档字符串)的构建时 2to3 转换,其方式与分发的方式不同: http://docs.python.org/dev/howto/pyporting#during-installation
I realize that I have replied to your secondary question without addressing unquestioned assumptions in your original problem:
You may, not need. Other strategies are described at http://docs.python.org/dev/howto/pyporting
You may :) distutils supports build-time 2to3 conversion for code (not docstrings), in a different manner that distribute’s: http://docs.python.org/dev/howto/pyporting#during-installation
在 2014 年底更新了这个问题,幸运的是 Python 打包混乱已经被 Continuum 的“conda”极大地清理了包管理器。
特别是,conda 可以快速创建 conda“环境”。您可以使用不同版本的 Python 配置环境。例如:
conda create -n py34 python=3.4 anaconda
conda create -n py26 python=2.6 anaconda
将创建两个(“py34”或“py26”)Python 环境不同版本的Python。
之后,您可以使用特定版本的 Python 调用环境:
source activate
在您必须处理不同版本的 Python 的情况下,此功能似乎特别有用。
此外,conda 具有以下功能:
最后一点尤其重要如果您从事科学计算领域。
Updating this question in late 2014 where fortunately the Python packaging chaos has been greatly cleaned up by Continuum's "conda" package manager.
In particular, conda quickly enables the creation of conda "environments". You can configure your environments with different versions of Python. For example:
conda create -n py34 python=3.4 anaconda
conda create -n py26 python=2.6 anaconda
will create two ("py34" or "py26") Python environments with different versions of Python.
Afterwards you can invoke the environment with the specific version of Python with:
source activate <env name>
This feature seems especially useful in your case where you are having to deal with different version of Python.
Moreover, conda has the following features:
That last point is especially important if you are in the scientific computing arena.