4

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

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

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

4

3 回答 3

4

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.

于 2020-06-25T06:19:39.470 回答
2

A.tar.gz是所谓的源分布。它包含你的包的源代码和如何构建它的说明,目标系统将在安装它之前执行构建。

A .wheelPEP 427中的规范详细信息)是一种构建的分发格式,这意味着目标系统不再需要构建它。安装轮子通常只是意味着将其内容复制到右侧site-packages

车轮听起来非常出色,因为它是。最好同时上传wheels 和源分发,因为任何构建的分发格式仅适用于目标系统的子集。对于只包含 python 代码的包,该子集就是“一切”——尽管人们仍然经常上传源代码分发,可能是为了在出现新标准的情况下向前兼容[1],可能是为了预测系统特定的扩展会突然需要为了支持所有平台的源代码分发,也许可以让用户选择运行具有特定构建参数的自定义构建。

观察不同情况的一个很好的示例包是numpy,它上传了 25 个轮子以覆盖最流行的平台,以及一个源代码分发。如果您从任何受支持的平台安装 numpy,您将获得一个不错的简短安装,只需几秒钟即可复制轮子的内容。如果你在一个不受支持的平台上(比如 alpine),一台普通的电脑可能需要至少 20 分钟从源码编译 numpy 才能安装,而且你需要各种系统级的开发工具来编译 C-安装的扩展。有点痛苦,但还是不能全部安装好。


[1]毕竟,在 wheel 有 egg 之前,如果包管理器决定只上传 egg 发行版而不上传源,那么采用 wheel 格式会比现在困难得多。

于 2020-06-25T06:22:32.550 回答
-3

您可以上传 whl 文件并使用以下命令进行安装

于 2020-06-25T05:17:16.117 回答