Distributing Python Modules分发Python模块

Email

distutils-sig@python.org

As a popular open source development project, Python has an active supporting community of contributors and users that also make their software available for other Python developers to use under open source license terms.作为一个流行的开放源码开发项目,Python拥有一个由贡献者和用户组成的积极支持社区,这些贡献者和用户还使他们的软件可供其他Python开发人员根据开放源码许可条款使用。

This allows Python users to share and collaborate effectively, benefiting from the solutions others have already created to common (and sometimes even rare!) problems, as well as potentially contributing their own solutions to the common pool.这允许Python用户有效地共享和协作,受益于其他人已经创建的通用(有时甚至罕见)解决方案问题,以及可能为公共池贡献自己的解决方案。

This guide covers the distribution part of the process. 本指南涵盖了流程的分发部分。For a guide to installing other Python projects, refer to the installation guide.有关安装其他Python项目的指南,请参阅安装指南

Note

For corporate and other institutional users, be aware that many organisations have their own policies around using and contributing to open source software. 对于公司和其他机构用户,请注意,许多组织在使用和贡献开源软件方面都有自己的政策。Please take such policies into account when making use of the distribution and installation tools provided with Python.在使用Python提供的分发和安装工具时,请考虑这些策略。

Key terms关键术语

  • the Python Package Index is a public repository of open source licensed packages made available for use by other Python usersPython包索引是开放源码许可包的公共存储库,可供其他Python用户使用

  • the Python Packaging Authority are the group of developers and documentation authors responsible for the maintenance and evolution of the standard packaging tools and the associated metadata and file format standards. Python打包机构是一组开发人员和文档作者,负责标准打包工具以及相关元数据和文件格式标准的维护和发展。They maintain a variety of tools, documentation and issue trackers on both GitHub and Bitbucket.他们在GitHubBitbucket上维护各种工具、文档和问题跟踪器。

  • distutils is the original build and distribution system first added to the Python standard library in 1998. 是最初的构建和分发系统,于1998年首次添加到Python标准库中。While direct use of distutils is being phased out, it still laid the foundation for the current packaging and distribution infrastructure, and it not only remains part of the standard library, but its name lives on in other ways (such as the name of the mailing list used to coordinate Python packaging standards development).虽然distutils的直接使用正在逐步停止,但它仍然为当前的打包和分发基础设施奠定了基础,它不仅仍然是标准库的一部分,而且其名称以其他方式存在(例如用于协调Python打包标准开发的邮件列表的名称)。

  • setuptools is a (largely) drop-in replacement for distutils first published in 2004. 是2004年首次发布的distutils的替代品(很大程度上)下降。Its most notable addition over the unmodified distutils tools was the ability to declare dependencies on other packages. 与未修改的distutils工具相比,它最显著的新增功能是能够声明对其他包的依赖关系。It is currently recommended as a more regularly updated alternative to distutils that offers consistent support for more recent packaging standards across a wide range of Python versions.目前建议将其作为distutils的更定期更新的替代方案,distutils在广泛的Python版本中为最新的打包标准提供一致的支持。

  • wheel (in this context) is a project that adds the bdist_wheel command to distutils/setuptools. (在本文中)是一个将bdist_wheel命令添加到distutils/setuptools的项目。This produces a cross platform binary packaging format (called “wheels” or “wheel files” and defined in PEP 427) that allows Python libraries, even those including binary extensions, to be installed on a system without needing to be built locally.这产生了一种跨平台的二进制打包格式(称为“轮子”或“轮子文件”,在PEP 427中定义),允许在系统上安装Python库,甚至包括二进制扩展,而无需在本地构建。

Open source licensing and collaboration开源许可和协作

In most parts of the world, software is automatically covered by copyright. This means that other developers require explicit permission to copy, use, modify and redistribute the software.在世界大多数地区,软件自动受版权保护。这意味着其他开发者需要明确的许可才能复制、使用、修改和重新发布软件。

Open source licensing is a way of explicitly granting such permission in a relatively consistent way, allowing developers to share and collaborate efficiently by making common solutions to various problems freely available. 开源许可是一种以相对一致的方式明确授予此类权限的方式,允许开发人员通过免费提供各种问题的通用解决方案来高效地共享和协作。This leaves many developers free to spend more time focusing on the problems that are relatively unique to their specific situation.这使得许多开发人员可以自由地花更多的时间专注于他们特定情况下相对独特的问题。

The distribution tools provided with Python are designed to make it reasonably straightforward for developers to make their own contributions back to that common pool of software if they choose to do so.Python提供的分发工具旨在使开发人员能够合理地直接将自己的贡献返回到公共软件池中(如果他们选择这样做的话)。

The same distribution tools can also be used to distribute software within an organisation, regardless of whether that software is published as open source software or not.同样的分发工具也可以用于在组织内分发软件,无论该软件是否作为开源软件发布。

Installing the tools安装工具

The standard library does not include build tools that support modern Python packaging standards, as the core development team has found that it is important to have standard tools that work consistently, even on older versions of Python.标准库不包括支持现代Python打包标准的构建工具,因为核心开发团队发现,即使在较旧的Python版本上,拥有一致工作的标准工具也很重要。

The currently recommended build and distribution tools can be installed by invoking the pip module at the command line:当前推荐的构建和分发工具可以通过在命令行调用pip模块来安装:

python -m pip install setuptools wheel twine

Note

For POSIX users (including macOS and Linux users), these instructions assume the use of a virtual environment.对于POSIX用户(包括macOS和Linux用户),这些指令假设使用虚拟环境

For Windows users, these instructions assume that the option to adjust the system PATH environment variable was selected when installing Python.对于Windows用户,这些说明假设在安装Python时选择了调整系统路径环境变量的选项。

The Python Packaging User Guide includes more details on the currently recommended tools.Python打包用户指南包含了关于当前推荐工具的更多详细信息。

Reading the Python Packaging User Guide阅读Python打包用户指南

The Python Packaging User Guide covers the various key steps and elements involved in creating and publishing a project:Python打包用户指南涵盖了创建和发布项目所涉及的各种关键步骤和元素:

How do I…?

These are quick answers or links for some common tasks.这些是一些常见任务的快速答案或链接。

choose a name for my project?为我的项目选择一个名称?

This isn’t an easy topic, but here are a few tips:这不是一个容易的话题,但这里有一些提示:

  • check the Python Package Index to see if the name is already in use检查Python包索引以查看该名称是否已在使用中

  • check popular hosting sites like GitHub, Bitbucket, etc to see if there is already a project with that name检查流行的托管网站,如GitHub、Bitbucket等,看看是否已有同名项目

  • check what comes up in a web search for the name you’re considering在网络搜索中查看你正在考虑的名字

  • avoid particularly common words, especially ones with multiple meanings, as they can make it difficult for users to find your software when searching for it避免使用特别常见的词,尤其是具有多种含义的词,因为它们会让用户在搜索软件时很难找到您的软件

create and distribute binary extensions?创建和分发二进制扩展?

This is actually quite a complex topic, with a variety of alternatives available depending on exactly what you’re aiming to achieve. 这实际上是一个相当复杂的话题,根据你的目标,有多种选择。See the Python Packaging User Guide for more information and recommendations.有关更多信息和建议,请参阅《Python打包用户指南》。