首页 > 解决方案 > PyPI 上上传的 Python 库的分发格式

问题描述

我完成了将包上传到https://test.pypi.org/的教程,我成功地这样做了。

但是,会在目录中$python setup.py sdist bdist_wheel生成一个.whl文件和一个tar.gz文件dist/twine允许仅上传.whltar.gz文件或两者。我在https://pypi.org/上看到许多存储库都上传了这两种格式。

我想了解什么是最佳做法。一种格式是否优于另一种格式?如果.whl文件足以分发我的代码,我是否tar.gz也应该上传文件?或者还有什么我在这里完全想念的。

标签: pythonpypipython-packagingtwine

解决方案


Best practice is to provide both.

A "built distribution" (.whl) for users that are able to use that distribution. This saves install time as a "built distribution" is pre-built and can just be dropped into place on the users machine, without any compilation step or without executing setup.py. There may be more than one built distribution for a given release -- once you start including compiled binaries with your distribution, they become platform-specific (see https://pypi.org/project/tensorflow/#files for example)

A "source distribution" (.tar.gz) is essentially a fallback for any user that cannot use your built distribution(s). Source distributions are not "built" meaning they may require compilation to install. At the minimum, they require executing a build-backend (for most projects, this is invoking setup.py with setuptools as the build-backend). Any installer should be able to install from source. In addition, a source distribution makes it easier for users who want to audit your source code (although this is possible with built distributions as well).

For the majority of Python projects, turning a "source distribution" into a "built distribution" results in a single pure-Python wheel (which is indicated by the none-any in a filename like projectname-1.2.3-py2.py3-none-any.whl). There's not much difference between this and the source distribution, but it's still best practice to upload both.


推荐阅读