安装 Python 模块¶
作为一个热门的开源开发项目,Python 拥有活跃的贡献者和用户支持社区并让他们的软件可供其他 Python 开发者在开源许可条款下使用。
这允许Python用户有效地共享和协作,从其他人已经创建的解决方案中受益于常见(有时甚至是罕见的)问题,以及可以提供他们自己的解决方案。
本指南涵盖了安装部分的流程。 有关创建和共享自己的 Python 项目的指导,请参阅 Python packaging user guide。
备注
对于企业和其他机构用户,请注意许多组织都有自己的政策来使用和贡献开源软件。在使用Python提供的分发和安装工具时,请考虑这些政策。
关键术语¶
pip is the preferred installer program. It is included by default with the Python binary installers.
virtual environment 是一种半隔离的 Python 环境,允许为特定的应用安装各自的包,而不是安装到整个系统。
venvis the standard tool for creating virtual environments. It defaults to installing pip into all created virtual environments.virtualenvis a third-party alternative (and predecessor) tovenv.The Python Package Index (PyPI) is a public repository of open source licensed packages made available for use by other Python users.
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. They maintain a variety of tools, documentation, and issue trackers on GitHub.
在 3.5 版本发生变更: 现在推荐使用 venv 来创建虚拟环境。
基本使用¶
标准打包工具完全是针对命令行使用方式来设计的。
The following command will install the latest version of a module and its dependencies from PyPI:
python -m pip install SomePackage
备注
对于 POSIX 用户(包括 macOS 和 Linux 用户)本指南中的示例假定使用了 virtual environment。
对于 Windows 用户,本指南中的示例假定在安装 Python 时选择了修改系统 PATH 环境变量。
在命令行中指定一个准确或最小版本也是可以的。 当使用比较运算符例如 >, < 或其他某些可以被终端所解析的特殊字符时,包名称与版本号应当用双引号括起来:
python -m pip install SomePackage==1.0.4 # 特定版本
python -m pip install "SomePackage>=1.0.4" # 最小版本
通常,如果一个匹配的模块已安装,尝试再次安装将不会有任何效果。 要升级现有模块必须显式地发出请求:
python -m pip install --upgrade SomePackage
More information and resources regarding pip and its capabilities can be found in the Python Packaging User Guide.
虚拟环境的创建可使用 venv 模块来完成。 向已激活虚拟环境安装软件包可使用上文所介绍的命令。
我应如何 ...?¶
这是一些常见任务的快速解答或相关链接。
... 只为当前用户安装软件包?¶
将 --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. 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, macOS, and other POSIX systems, use the versioned Python commands
in combination with the -m switch to run the appropriate copy of
pip:
python3 -m pip install SomePackage # default Python 3
python3.14 -m pip install SomePackage # specifically Python 3.14
Appropriately versioned pip commands may also be available.
On Windows, use the py Python launcher in combination with the -m
switch:
py -3 -m pip install SomePackage # default Python 3
py -3.14 -m pip install SomePackage # specifically Python 3.14
常见的安装问题¶
在 Linux 的系统 Python 版本上安装¶
On Linux systems, a Python installation will typically be included as part of the distribution. 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.
On such systems, it is often better to use a virtual environment or a per-user installation when installing packages with pip.
未安装 pip¶
It is possible that pip does not get installed by default. One potential fix is:
python -m ensurepip --default-pip
There are also additional resources for installing pip.
安装二进制编译扩展¶
Python once relied heavily on source-based distribution, with end users being expected to compile extension modules from source as part of the installation process.
With the introduction of the binary wheel format, and the ability to publish wheels through PyPI, this problem is diminishing, as users are more regularly able to install pre-built extensions rather than needing to build them themselves.
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.