“Python/docs/3.9/installing/index”的版本间差异
(autoload) |
小 (Page commit) |
||
第1行: | 第1行: | ||
+ | {{DISPLAYTITLE:安装 Python 模块 — Python 文档}} | ||
<div id="installing-python-modules" class="section"> | <div id="installing-python-modules" class="section"> | ||
<span id="installing-index"></span> | <span id="installing-index"></span> | ||
− | = | + | = 安装 Python 模块 = |
− | ; | + | ; 电子邮件 |
: [mailto:distutils-sig%40python.org distutils-sig<span>@</span>python<span>.</span>org] | : [mailto:distutils-sig%40python.org distutils-sig<span>@</span>python<span>.</span>org] | ||
− | + | 作为一个流行的开源开发项目,Python 拥有一个由贡献者和用户组成的活跃支持社区,这些社区还可以让其他 Python 开发人员根据开源许可条款使用他们的软件。 | |
− | |||
− | |||
− | + | 这允许 Python 用户有效地共享和协作,从其他人已经为常见(有时甚至是罕见的!)问题创建的解决方案中受益,并可能将他们自己的解决方案贡献给公共池。 | |
− | |||
− | |||
− | |||
− | + | 本指南涵盖了该过程的安装部分。 有关创建和共享您自己的 Python 项目的指南,请参阅 [[../../distributing/index#distributing-index|分发指南]] 。 | |
− | |||
− | [[../../distributing/index#distributing-index| | ||
<div class="admonition note"> | <div class="admonition note"> | ||
− | + | 笔记 | |
− | + | 对于企业和其他机构用户,请注意,许多组织在使用和贡献开源软件方面都有自己的政策。 在使用 Python 提供的分发和安装工具时,请考虑这些策略。 | |
− | |||
− | |||
− | |||
第33行: | 第24行: | ||
<div id="key-terms" class="section"> | <div id="key-terms" class="section"> | ||
− | == | + | == 关键术语 == |
− | * <code>pip</code> | + | * <code>pip</code> 是首选的安装程序。 从 Python 3.4 开始,它默认包含在 Python 二进制安装程序中。 |
− | * | + | * ''虚拟环境'' 是一个半隔离的 Python 环境,它允许安装包以供特定应用程序使用,而不是在系统范围内安装。 |
− | * <code>venv</code> | + | * <code>venv</code> 是创建虚拟环境的标准工具,自 Python 3.3 以来一直是 Python 的一部分。 从 Python 3.4 开始,它默认将 <code>pip</code> 安装到所有创建的虚拟环境中。 |
− | * <code>virtualenv</code> | + | * <code>virtualenv</code> 是 <code>venv</code> 的第三方替代品(和前身)。 它允许在 3.4 之前的 Python 版本上使用虚拟环境,这些版本要么根本不提供 <code>venv</code>,要么无法自动将 <code>pip</code> 安装到创建的环境中。 |
− | * | + | * [https://pypi.org Python 包索引] 是开源许可包的公共存储库,可供其他 Python 用户使用。 |
− | * | + | * [https://www.pypa.io/ Python Packaging Authority] 是一组开发人员和文档作者,负责维护和发展标准打包工具以及相关的元数据和文件格式标准。 他们在 [https://github.com/pypa GitHub] 和 [https://bitbucket.org/pypa/ Bitbucket] 上维护各种工具、文档和问题跟踪器。 |
− | * <code>distutils</code> | + | * <code>distutils</code> 是 1998 年首次添加到 Python 标准库中的原始构建和分发系统。 虽然直接使用 <code>distutils</code> 正在逐步淘汰,但它仍然为当前的打包和分发基础设施奠定了基础,它不仅仍然是标准库的一部分,而且它的名字还在其他方面(例如用于协调 Python 打包标准开发的邮件列表的名称)。 |
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | <span class="versionmodified changed"> | + | <span class="versionmodified changed"> 3.5 版更改: </span> 现在推荐使用 <code>venv</code> 来创建虚拟环境。 |
第51行: | 第42行: | ||
<div class="admonition seealso"> | <div class="admonition seealso"> | ||
− | + | 也可以看看 | |
− | [https://packaging.python.org/installing/#creating-virtual-environments Python | + | [https://packaging.python.org/installing/#creating-virtual-environments Python 打包用户指南:创建和使用虚拟环境] |
第61行: | 第52行: | ||
<div id="basic-usage" class="section"> | <div id="basic-usage" class="section"> | ||
− | == | + | == 基本用法 == |
− | + | 标准打包工具均设计为可从命令行使用。 | |
− | |||
− | + | 以下命令将从 Python Package Index 安装最新版本的模块及其依赖项: | |
− | |||
<div class="highlight-none notranslate"> | <div class="highlight-none notranslate"> | ||
第73行: | 第62行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | <pre>python -m pip install SomePackage</pre> | + | <pre class="none">python -m pip install SomePackage</pre> |
</div> | </div> | ||
第80行: | 第69行: | ||
<div class="admonition note"> | <div class="admonition note"> | ||
− | + | 笔记 | |
− | + | 对于 POSIX 用户(包括 macOS 和 Linux 用户),本指南中的示例假设使用 [[../../glossary#term-virtual-environment|虚拟环境]] 。 | |
− | |||
− | + | 对于 Windows 用户,本指南中的示例假定在安装 Python 时选择了调整系统 PATH 环境变量的选项。 | |
− | |||
− | |||
</div> | </div> | ||
− | + | 也可以直接在命令行上指定精确或最低版本。 当使用比较器操作符如 <code>></code>、<code><</code> 或其他一些被 shell 解释的特殊字符时,包名和版本应该用双引号括起来: | |
− | |||
− | |||
− | |||
<div class="highlight-none notranslate"> | <div class="highlight-none notranslate"> | ||
第100行: | 第83行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | <pre>python -m pip install SomePackage==1.0.4 # specific version | + | <pre class="none">python -m pip install SomePackage==1.0.4 # specific version |
python -m pip install "SomePackage>=1.0.4" # minimum version</pre> | python -m pip install "SomePackage>=1.0.4" # minimum version</pre> | ||
第106行: | 第89行: | ||
</div> | </div> | ||
− | + | 通常,如果已经安装了合适的模块,则再次尝试安装它不会有任何效果。 必须明确请求升级现有模块: | |
− | |||
− | |||
<div class="highlight-none notranslate"> | <div class="highlight-none notranslate"> | ||
第114行: | 第95行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | <pre>python -m pip install --upgrade SomePackage</pre> | + | <pre class="none">python -m pip install --upgrade SomePackage</pre> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 有关 <code>pip</code> 及其功能的更多信息和资源可以在 [https://packaging.python.org Python 打包用户指南] 中找到。 | |
− | |||
− | + | 虚拟环境的创建是通过 [[../../library/venv#module-venv|venv]] 模块完成的。 使用上面显示的命令将软件包安装到活动的虚拟环境中。 | |
− | |||
− | |||
<div class="admonition seealso"> | <div class="admonition seealso"> | ||
− | + | 也可以看看 | |
− | [https://packaging.python.org/installing/ Python | + | [https://packaging.python.org/installing/ Python 打包用户指南:安装 Python 分发包] |
第138行: | 第116行: | ||
<div id="how-do-i" class="section"> | <div id="how-do-i" class="section"> | ||
− | == | + | == 我如何能 …? == |
− | + | 这些是一些常见任务的快速答案或链接。 | |
<div id="install-pip-in-versions-of-python-prior-to-python-3-4" class="section"> | <div id="install-pip-in-versions-of-python-prior-to-python-3-4" class="section"> | ||
− | === | + | === … 在 Python 3.4 之前的 Python 版本中安装 pip? === |
− | Python | + | Python 才开始将 <code>pip</code> 与 Python 3.4 捆绑在一起。 对于早期版本,<code>pip</code> 需要按照 Python 打包用户指南中的说明进行“引导”。 |
− | <code>pip</code> | ||
− | |||
<div class="admonition seealso"> | <div class="admonition seealso"> | ||
− | + | 也可以看看 | |
− | [https://packaging.python.org/installing/#requirements-for-installing-packages Python | + | [https://packaging.python.org/installing/#requirements-for-installing-packages Python 打包用户指南:安装包的要求] |
第162行: | 第138行: | ||
<div id="install-packages-just-for-the-current-user" class="section"> | <div id="install-packages-just-for-the-current-user" class="section"> | ||
− | === ... | + | === ... 只为当前用户安装软件包? === |
− | + | 将 <code>--user</code> 选项传递给 <code>python -m pip install</code> 将只为当前用户安装一个包,而不是为系统的所有用户安装。 | |
− | |||
第171行: | 第146行: | ||
<div id="install-scientific-python-packages" class="section"> | <div id="install-scientific-python-packages" class="section"> | ||
− | === | + | === … 安装科学的 Python 包? === |
− | + | 许多科学 Python 包具有复杂的二进制依赖项,目前直接使用 <code>pip</code> 进行安装并不容易。 此时,用户通过 [https://packaging.python.org/science/ 其他方式] 安装这些软件包通常会比尝试使用 <code>pip</code> 安装它们更容易。 | |
− | |||
− | |||
− | [https://packaging.python.org/science/ | ||
− | |||
<div class="admonition seealso"> | <div class="admonition seealso"> | ||
− | + | 也可以看看 | |
− | [https://packaging.python.org/science/ Python | + | [https://packaging.python.org/science/ Python 打包用户指南:安装科学包] |
第191行: | 第162行: | ||
<div id="work-with-multiple-versions-of-python-installed-in-parallel" class="section"> | <div id="work-with-multiple-versions-of-python-installed-in-parallel" class="section"> | ||
− | === ... | + | === ... 使用并行安装的多个 Python 版本? === |
− | + | 在 Linux、macOS 和其他 POSIX 系统上,将版本化 Python 命令与 <code>-m</code> 开关结合使用以运行 <code>pip</code> 的相应副本: | |
− | |||
− | <code>pip</code> | ||
<div class="highlight-none notranslate"> | <div class="highlight-none notranslate"> | ||
第201行: | 第170行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | <pre>python2 -m pip install SomePackage # default Python 2 | + | <pre class="none">python2 -m pip install SomePackage # default Python 2 |
python2.7 -m pip install SomePackage # specifically Python 2.7 | python2.7 -m pip install SomePackage # specifically Python 2.7 | ||
python3 -m pip install SomePackage # default Python 3 | python3 -m pip install SomePackage # default Python 3 | ||
第209行: | 第178行: | ||
</div> | </div> | ||
− | + | 也可以使用适当版本的 <code>pip</code> 命令。 | |
− | + | 在 Windows 上,将 <code>py</code> Python 启动器与 <code>-m</code> 开关结合使用: | |
− | |||
<div class="highlight-none notranslate"> | <div class="highlight-none notranslate"> | ||
第218行: | 第186行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | <pre>py -2 -m pip install SomePackage # default Python 2 | + | <pre class="none">py -2 -m pip install SomePackage # default Python 2 |
py -2.7 -m pip install SomePackage # specifically Python 2.7 | py -2.7 -m pip install SomePackage # specifically Python 2.7 | ||
py -3 -m pip install SomePackage # default Python 3 | py -3 -m pip install SomePackage # default Python 3 | ||
第232行: | 第200行: | ||
<div id="common-installation-issues" class="section"> | <div id="common-installation-issues" class="section"> | ||
− | == | + | == 常见安装问题 == |
<div id="installing-into-the-system-python-on-linux" class="section"> | <div id="installing-into-the-system-python-on-linux" class="section"> | ||
− | === | + | === 在 Linux 上安装到系统 Python === |
− | + | 在 Linux 系统上,Python 安装通常会包含在发行版中。 安装到此 Python 安装中需要对系统进行 root 访问,如果使用 <code>pip</code> 意外升级某个组件,则可能会干扰系统包管理器和系统其他组件的运行。 | |
− | |||
− | root | ||
− | |||
− | |||
− | + | 在此类系统上,使用 <code>pip</code> 安装软件包时,通常最好使用虚拟环境或按用户安装。 | |
− | |||
第251行: | 第214行: | ||
<div id="pip-not-installed" class="section"> | <div id="pip-not-installed" class="section"> | ||
− | === | + | === pip 未安装 === |
− | + | 默认情况下可能未安装 <code>pip</code>。 一种可能的解决方法是: | |
<div class="highlight-none notranslate"> | <div class="highlight-none notranslate"> | ||
第259行: | 第222行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | <pre>python -m ensurepip --default-pip</pre> | + | <pre class="none">python -m ensurepip --default-pip</pre> |
</div> | </div> | ||
</div> | </div> | ||
− | + | [https://packaging.python.org/tutorials/installing-packages/#install-pip-setuptools-and-wheel 安装 pip.] 也有额外的资源 | |
第270行: | 第233行: | ||
<div id="installing-binary-extensions" class="section"> | <div id="installing-binary-extensions" class="section"> | ||
− | === | + | === 安装二进制扩展 === |
− | Python | + | Python 通常严重依赖基于源代码的分发,最终用户需要从源代码编译扩展模块作为安装过程的一部分。 |
− | |||
− | |||
− | + | 随着对二进制 <code>wheel</code> 格式的支持的引入,以及通过 Python 包索引发布至少适用于 Windows 和 macOS 的轮子的能力,这个问题预计会随着时间的推移而减少,因为用户可以更经常地安装预先构建的扩展,而不是需要自己构建它们。 | |
− | |||
− | |||
− | |||
− | |||
− | + | 一些用于安装 [https://packaging.python.org/science/ 科学软件] 的解决方案尚未作为预构建的 <code>wheel</code> 文件提供,也可能有助于获取其他二进制扩展,而无需在本地构建它们。 | |
− | |||
− | |||
<div class="admonition seealso"> | <div class="admonition seealso"> | ||
− | + | 也可以看看 | |
− | [https://packaging.python.org/extensions/ Python | + | [https://packaging.python.org/extensions/ Python 打包用户指南:二进制扩展] |
第300行: | 第255行: | ||
</div> | </div> | ||
+ | <div class="clearer"> | ||
− | [[Category:Python 3.9 | + | |
+ | |||
+ | </div> | ||
+ | |||
+ | [[Category:Python 3.9 文档]] |
2021年10月31日 (日) 04:51的最新版本
安装 Python 模块
作为一个流行的开源开发项目,Python 拥有一个由贡献者和用户组成的活跃支持社区,这些社区还可以让其他 Python 开发人员根据开源许可条款使用他们的软件。
这允许 Python 用户有效地共享和协作,从其他人已经为常见(有时甚至是罕见的!)问题创建的解决方案中受益,并可能将他们自己的解决方案贡献给公共池。
本指南涵盖了该过程的安装部分。 有关创建和共享您自己的 Python 项目的指南,请参阅 分发指南 。
笔记
对于企业和其他机构用户,请注意,许多组织在使用和贡献开源软件方面都有自己的政策。 在使用 Python 提供的分发和安装工具时,请考虑这些策略。
关键术语
pip
是首选的安装程序。 从 Python 3.4 开始,它默认包含在 Python 二进制安装程序中。- 虚拟环境 是一个半隔离的 Python 环境,它允许安装包以供特定应用程序使用,而不是在系统范围内安装。
venv
是创建虚拟环境的标准工具,自 Python 3.3 以来一直是 Python 的一部分。 从 Python 3.4 开始,它默认将pip
安装到所有创建的虚拟环境中。virtualenv
是venv
的第三方替代品(和前身)。 它允许在 3.4 之前的 Python 版本上使用虚拟环境,这些版本要么根本不提供venv
,要么无法自动将pip
安装到创建的环境中。- Python 包索引 是开源许可包的公共存储库,可供其他 Python 用户使用。
- Python Packaging Authority 是一组开发人员和文档作者,负责维护和发展标准打包工具以及相关的元数据和文件格式标准。 他们在 GitHub 和 Bitbucket 上维护各种工具、文档和问题跟踪器。
distutils
是 1998 年首次添加到 Python 标准库中的原始构建和分发系统。 虽然直接使用distutils
正在逐步淘汰,但它仍然为当前的打包和分发基础设施奠定了基础,它不仅仍然是标准库的一部分,而且它的名字还在其他方面(例如用于协调 Python 打包标准开发的邮件列表的名称)。
3.5 版更改: 现在推荐使用 venv
来创建虚拟环境。
基本用法
标准打包工具均设计为可从命令行使用。
以下命令将从 Python Package Index 安装最新版本的模块及其依赖项:
python -m pip install SomePackage
笔记
对于 POSIX 用户(包括 macOS 和 Linux 用户),本指南中的示例假设使用 虚拟环境 。
对于 Windows 用户,本指南中的示例假定在安装 Python 时选择了调整系统 PATH 环境变量的选项。
也可以直接在命令行上指定精确或最低版本。 当使用比较器操作符如 >
、<
或其他一些被 shell 解释的特殊字符时,包名和版本应该用双引号括起来:
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 打包用户指南 中找到。
虚拟环境的创建是通过 venv 模块完成的。 使用上面显示的命令将软件包安装到活动的虚拟环境中。
我如何能 …?
这些是一些常见任务的快速答案或链接。
… 在 Python 3.4 之前的 Python 版本中安装 pip?
Python 才开始将 pip
与 Python 3.4 捆绑在一起。 对于早期版本,pip
需要按照 Python 打包用户指南中的说明进行“引导”。
... 只为当前用户安装软件包?
将 --user
选项传递给 python -m pip install
将只为当前用户安装一个包,而不是为系统的所有用户安装。
… 安装科学的 Python 包?
许多科学 Python 包具有复杂的二进制依赖项,目前直接使用 pip
进行安装并不容易。 此时,用户通过 其他方式 安装这些软件包通常会比尝试使用 pip
安装它们更容易。
... 使用并行安装的多个 Python 版本?
在 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
也可以使用适当版本的 pip
命令。
在 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 包索引发布至少适用于 Windows 和 macOS 的轮子的能力,这个问题预计会随着时间的推移而减少,因为用户可以更经常地安装预先构建的扩展,而不是需要自己构建它们。
一些用于安装 科学软件 的解决方案尚未作为预构建的 wheel
文件提供,也可能有助于获取其他二进制扩展,而无需在本地构建它们。