截至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等之间的区别是什么?(是的,我知道,我和你一起抱怨。)