安裝 Python 模組

电子邮箱:distutils-sig@python.org

作为一个流行的开源开发项目,Python拥有一个活跃的贡献者和用户支持社区,这些社区也可以让他们的软件可供其他Python开发人员在开源许可条款下使用。

这允许Python用户有效地共享和协作,从其他人已经创建的解决方案中受益于常见(有时甚至是罕见的)问题,以及可以提供他们自己的解决方案。

本指南涵盖了分发部分的流程。有关安装其他Python项目的指南,请参阅 安装指南

備註

对于企业和其他机构用户,请注意许多组织都有自己的政策来使用和贡献开源软件。在使用Python提供的分发和安装工具时,请考虑这些政策。

关键术语

  • 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
  • pyvenv is the standard tool for creating virtual environments, and has been part of Python since Python 3.3. Starting with Python 3.4, it defaults to installing pip into all created virtual environments
  • virtualenv is a third party alternative (and predecessor) to pyvenv. It allows virtual environments to be used on versions of Python prior to 3.4, which either don’t provide pyvenv at all, or aren’t able to automatically install pip into created environments.
  • the Python Packaging Index is a public repository of open source licensed packages made available for use by other Python users
  • 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. They maintain a variety of tools, documentation and issue trackers on both GitHub and BitBucket.
  • distutils 是最初的构建和分发系统,于 1998 年首次加入 Python 标准库。 虽然直接使用 distutils 的方式已被淘汰,它仍然是当前打包和分发架构的基础,而且它不仅仍然是标准库的一部分,这个名称还以其他方式存在(例如用于协调 Python 打包标准开发流程的邮件列表就以此命名)。

基本使用

标准打包工具完全是针对命令行使用方式来设计的。

以下命令将从 Python Packaging Index 安装一个模块的最新版本及其依赖项:

python -m pip install SomePackage

備註

对于 POSIX 用户(包括 Mac OS X 和 Linux 用户)本指南中的示例假定使用了 virtual environment

对于 Windows 用户,本指南中的示例假定在安装 Python 时选择了修改系统 PATH 环境变量。

在命令行中指定一个准确或最小版本也是可以的。 当使用比较运算符例如 >, < 或其他某些可以被终端所解析的特殊字符时,包名称与版本号应当用双引号括起来:

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

通常,如果一个匹配的模块已安装,尝试再次安装将不会有任何效果。 要升级现有模块必须显式地发出请求:

python -m pip install --upgrade SomePackage

更多有关 pip 及其功能的信息和资源可以在 Python 软件包用户指南 中找到。

pyvenv has its own documentation at pyvenv - Creating virtual environments. Installing into an active virtual environment uses the commands shown above.

我应如何 …?

这是一些常见任务的快速解答或相关链接。

… 在 Python 3.4 之前的 Python 版本中安装 pip

Python 捆绑 pip 是从 Python 3.4 才开始的。 对于更早的版本,pip 需要“引导安装bootstrapped”,具体说明参见 Python 软件包用户指南。

… 只为当前用户安装软件包?

--user 选项传入 python -m pip install 将只为当前用户而非为系统中的所有用户安装软件包。

… 安装科学计算类 Python 软件包?

A number of scientific Python packages have complex binary dependencies, and aren’t currently easy to install using pip directly. 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.

… 使用并行安装的多个 Python 版本?

On Linux, Mac OS X and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of 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)

在 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

常见的安装问题

在 Linux 的系统 Python 版本上安装

Linux 系统通常会将某个 Python 版本作为发行版的一部分包含在内。 将软件包安装到这个 Python 版本上需要系统 root 权限,并可能会干扰到系统包管理器和其他系统组件的运作,如果这些组件在使用 pip 时被意外升级的话。

在这样的系统上,通过 pip 安装软件包通常最好是使用虚拟环境或分用户安装。

安装二进制编译扩展

Python 通常非常依赖基于源代码的发布方式,也就是期望最终用户在安装过程中使用源码来编译生成扩展模块。

随着对二进制码 wheel 格式支持的引入,以及通过 Python Packaging Index 至少发布 Windows 和 Mac OS X 版的 wheel 文件,预计此问题将逐步得到解决,因为用户将能够更频繁地安装预编译扩展,而不再需要自己编译它们。

Some of the solutions for installing scientific software that is not yet available as pre-built wheel files may also help with obtaining other binary extensions without needing to build them locally.