Installing 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 installation part of the process. 本指南涵盖了该过程的安装部分。For a guide to creating and sharing your own Python projects, refer to the distribution 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

  • pip is the preferred installer program. Starting with Python 3.4, it is included by default with the Python binary installers.pip是首选的安装程序。从Python 3.4开始,它默认包含在Python二进制安装程序中。

  • A virtual environment is a semi-isolated Python environment that allows packages to be installed for use by a particular application, rather than being installed system wide.虚拟环境是一个半隔离的Python环境,它允许安装包供特定应用程序使用,而不是安装在系统范围内。

  • venv is the standard tool for creating virtual environments, and has been part of Python since Python 3.3. 是创建虚拟环境的标准工具,自Python 3.3以来一直是Python的一部分。Starting with Python 3.4, it defaults to installing pip into all created virtual environments.从Python 3.4开始,它默认将pip安装到所有创建的虚拟环境中。

  • virtualenv is a third party alternative (and predecessor) to venv. virtualenvvenv的第三方替代品(和前身)。It allows virtual environments to be used on versions of Python prior to 3.4, which either don’t provide venv at all, or aren’t able to automatically install pip into created environments.它允许在3.4之前的Python版本上使用虚拟环境,这些版本要么根本不提供venv,要么无法将pip自动安装到创建的环境中。

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

  • the Python Packaging Authority is 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. 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).是最初的构建和分发系统,于1998年首次添加到Python标准库中。虽然distutils的直接使用正在逐步淘汰,但它仍然为当前的打包和分发基础设施奠定了基础,而且它不仅仍然是标准库的一部分,而且它的名称以其他方式存在(例如用于协调Python打包标准开发的邮件列表的名称)。

Changed in version 3.5: 在版本3.5中更改:The use of venv is now recommended for creating virtual environments.现在建议使用venv创建虚拟环境。

Basic usage基本用途

The standard packaging tools are all designed to be used from the command line.标准打包工具都是为从命令行使用而设计的。

The following command will install the latest version of a module and its dependencies from the Python Package Index:以下命令将从Python包索引中安装最新版本的模块及其依赖项:

python -m pip install SomePackage

Note

For POSIX users (including macOS and Linux users), the examples in this guide assume the use of a virtual environment.对于POSIX用户(包括macOS和Linux用户),本指南中的示例假定使用虚拟环境

For Windows users, the examples in this guide assume that the option to adjust the system PATH environment variable was selected when installing Python.对于Windows用户,本指南中的示例假定在安装Python时选择了调整系统路径环境变量的选项。

It’s also possible to specify an exact or minimum version directly on the command line. 也可以直接在命令行上指定精确或最低版本。When using comparator operators such as >, < or some other special character which get interpreted by shell, the package name and the version should be enclosed within double quotes:当使用比较运算符(如><或shell解释的其他特殊字符)时,包名称和版本应包含在双引号内:

python -m pip install SomePackage==1.0.4    # specific version
python -m pip install "SomePackage>=1.0.4" # minimum version

Normally, if a suitable module is already installed, attempting to install it again will have no effect. 通常,如果已经安装了合适的模块,尝试再次安装不会有任何效果。Upgrading existing modules must be requested explicitly:必须明确要求升级现有模块:

python -m pip install --upgrade SomePackage

More information and resources regarding pip and its capabilities can be found in the Python Packaging User Guide.有关pip及其功能的更多信息和资源可以在Python打包用户指南中找到。

Creation of virtual environments is done through the venv module. 通过venv模块创建虚拟环境。Installing packages into an active virtual environment uses the commands shown above.使用上面显示的命令将包安装到活动虚拟环境中。

How do I …?我该怎么做?

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

… install pip in versions of Python prior to Python 3.4?在Python 3.4之前的Python版本中安装pip

Python only started bundling pip with Python 3.4. Python只是开始将pip与Python 3.4捆绑在一起。For earlier versions, pip needs to be “bootstrapped” as described in the Python Packaging User Guide.对于早期版本,pip需要按照Python打包用户指南中的描述进行“引导”。

… install packages just for the current user?是否仅为当前用户安装软件包?

Passing the --user option to python -m pip install will install a package just for the current user, rather than for all users of the system.--user选项传递给python -m pip install将只为当前用户安装一个包,而不是为系统的所有用户安装。

… install scientific Python packages?安装scientific Python软件包?

A number of scientific Python packages have complex binary dependencies, and aren’t currently easy to install using pip directly. 许多scientific Python软件包具有复杂的二进制依赖关系,目前直接使用pip安装并不容易。At this point in time, it will often be easier for users to install these packages by other means rather than attempting to install them with pip.此时,用户通常更容易通过其他方式安装这些软件包,而不是尝试使用pip安装它们。

… work with multiple versions of Python installed in parallel?使用多个并行安装的Python版本?

On Linux, macOS, and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:在Linux、macOS和其他POSIX系统上,使用经过版本控制的Python命令和-m开关来运行适当的pip副本:

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4

Appropriately versioned pip commands may also be available.也可以使用版本适当的pip命令。

On Windows, use the py Python launcher in combination with the -m switch:在Windows上,将py Python启动器与-m开关结合使用:

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4

Common installation issues常见安装问题

Installing into the system Python on Linux在Linux上安装到系统Python中

On Linux systems, a Python installation will typically be included as part of the distribution. 在Linux系统上,Python安装通常作为发行版的一部分。Installing into this Python installation requires root access to the system, and may interfere with the operation of the system package manager and other components of the system if a component is unexpectedly upgraded using pip.安装到此Python安装中需要对系统进行root访问,如果使用pip意外升级组件,则可能会干扰系统包管理器和系统其他组件的操作。

On such systems, it is often better to use a virtual environment or a per-user installation when installing packages with pip.在这样的系统上,在安装带有pip的包时,最好使用虚拟环境或每用户安装。

Pip not installed未安装Pip

It is possible that pip does not get installed by default. 默认情况下,可能不会安装pipOne potential fix is:一种可能的修复方法是:

python -m ensurepip --default-pip

There are also additional resources for installing pip.还有其他资源可用于安装pip

Installing binary extensions安装二进制扩展

Python has typically relied heavily on source based distribution, with end users being expected to compile extension modules from source as part of the installation process.Python通常严重依赖于基于源代码的分发,最终用户需要在安装过程中从源代码编译扩展模块。

With the introduction of support for the binary wheel format, and the ability to publish wheels for at least Windows and macOS through the Python Package Index, this problem is expected to diminish over time, as users are more regularly able to install pre-built extensions rather than needing to build them themselves.随着对二进制wheel格式的支持的引入,以及通过Python软件包索引发布至少Windows和macOS的wheel的能力,随着时间的推移,这个问题预计会逐渐减少,因为用户可以更经常地安装预构建的扩展,而无需自己构建它们。

Some of the solutions for installing scientific software that are not yet available as pre-built wheel files may also help with obtaining other binary extensions without needing to build them locally.安装科学软件的一些解决方案还没有作为预构建的轮子文件提供,这些解决方案还可以帮助获得其他二进制扩展,而无需在本地构建它们。