“Python/docs/3.9/tutorial/venv”的版本间差异
(autoload) |
小 (Page commit) |
||
第1行: | 第1行: | ||
+ | {{DISPLAYTITLE:12. 虚拟环境和包 — Python 文档}} | ||
<div id="virtual-environments-and-packages" class="section"> | <div id="virtual-environments-and-packages" class="section"> | ||
<span id="tut-venv"></span> | <span id="tut-venv"></span> | ||
− | = | + | = 12. 虚拟环境和包 = |
<div id="introduction" class="section"> | <div id="introduction" class="section"> | ||
− | == | + | == 12.1. 介绍 == |
− | Python | + | Python 应用程序通常会使用标准库中没有的包和模块。 应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者应用程序可能使用库接口的过时版本编写。 |
− | |||
− | |||
− | |||
− | |||
− | + | 这意味着一个 Python 安装可能无法满足每个应用程序的要求。 如果应用程序 A 需要特定模块的 1.0 版,而应用程序 B 需要 2.0 版,则需求存在冲突,安装 1.0 或 2.0 版将导致一个应用程序无法运行。 | |
− | |||
− | 1.0 | ||
− | |||
− | |||
− | + | 此问题的解决方案是创建一个 [[../../glossary#term-virtual-environment|虚拟环境]] ,一个自包含的目录树,其中包含针对特定 Python 版本的 Python 安装,以及一些附加包。 | |
− | |||
− | |||
− | + | 不同的应用程序可以使用不同的虚拟环境。 为了解决前面的冲突需求示例,应用程序 A 可以拥有自己的安装了 1.0 版的虚拟环境,而应用程序 B 拥有另一个安装了 2.0 版的虚拟环境。 如果应用程序 B 需要将库升级到版本 3.0,这不会影响应用程序 A 的环境。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
第35行: | 第21行: | ||
<div id="creating-virtual-environments" class="section"> | <div id="creating-virtual-environments" class="section"> | ||
− | == | + | == 12.2. 创建虚拟环境 == |
− | + | 用于创建和管理虚拟环境的模块称为 [[../../library/venv#module-venv|venv]]。 [[../../library/venv#module-venv|venv]] 通常会安装您可用的最新版本的 Python。 如果您的系统上有多个 Python 版本,您可以通过运行 <code>python3</code> 或您想要的任何版本来选择特定的 Python 版本。 | |
− | [[../../library/venv#module-venv| | ||
− | |||
− | |||
− | |||
− | + | 要创建虚拟环境,请确定要放置它的目录,然后使用目录路径将 [[../../library/venv#module-venv|venv]] 模块作为脚本运行: | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第50行: | 第31行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">python3 -m venv tutorial-env</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 这将创建 <code>tutorial-env</code> 目录(如果它不存在),并在其中创建包含 Python 解释器、标准库和各种支持文件的副本的目录。 | |
− | |||
− | |||
− | + | 虚拟环境的常见目录位置是 <code>.venv</code>。 该名称通常将目录隐藏在您的 shell 中,从而使其不受干扰,同时为其提供一个名称来解释该目录存在的原因。 它还可以防止与某些工具支持的 <code>.env</code> 环境变量定义文件发生冲突。 | |
− | |||
− | |||
− | |||
− | |||
− | + | 创建虚拟环境后,您可以激活它。 | |
− | + | 在 Windows 上,运行: | |
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第73行: | 第48行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">tutorial-env\Scripts\activate.bat</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 在 Unix 或 MacOS 上,运行: | |
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第84行: | 第59行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">source tutorial-env/bin/activate</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | (此脚本是为 bash shell 编写的。 如果您使用 '''csh''' 或 '''fish''' 外壳,则应使用替代的 <code>activate.csh</code> 和 <code>activate.fish</code> 脚本。) | |
− | '''csh''' | ||
− | <code>activate.csh</code> | ||
− | |||
− | + | 激活虚拟环境将更改您的 shell 提示以显示您正在使用的虚拟环境,并修改环境以便运行 <code>python</code> 将为您提供特定版本的 Python 和安装。 例如: | |
− | |||
− | <code>python</code> | ||
− | |||
<div class="highlight-bash notranslate"> | <div class="highlight-bash notranslate"> | ||
第103行: | 第72行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="bash">$ source ~/envs/tutorial-env/bin/activate |
(tutorial-env) $ python | (tutorial-env) $ python | ||
Python 3.5.1 (default, May 6 2016, 10:59:36) | Python 3.5.1 (default, May 6 2016, 10:59:36) | ||
... | ... | ||
− | + | >>> import sys | |
− | + | >>> sys.path | |
['', '/usr/local/lib/python35.zip', ..., | ['', '/usr/local/lib/python35.zip', ..., | ||
'~/envs/tutorial-env/lib/python3.5/site-packages'] | '~/envs/tutorial-env/lib/python3.5/site-packages'] | ||
− | + | >>></syntaxhighlight> | |
</div> | </div> | ||
第120行: | 第89行: | ||
<div id="managing-packages-with-pip" class="section"> | <div id="managing-packages-with-pip" class="section"> | ||
− | == | + | == 12.3. 使用 pip 管理包 == |
− | + | 您可以使用名为 '''pip''' 的程序安装、升级和删除软件包。 默认情况下<code>pip</code>将从 Python Package Index 安装包,< https://pypi.org >. 您可以通过在 Web 浏览器中访问 Python 包索引来浏览它。 | |
− | '''pip''' | ||
− | Package Index | ||
− | |||
− | |||
− | < | + | <code>pip</code> 有多个子命令:“install”、“uninstall”、“freeze”等。 (有关 <code>pip</code> 的完整文档,请参阅 [[../../installing/index#installing-index|安装 Python 模块]] 指南。) |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | <code>pip</code> | ||
− | |||
− | |||
− | + | 您可以通过指定包的名称来安装最新版本的包: | |
<div class="highlight-bash notranslate"> | <div class="highlight-bash notranslate"> | ||
第153行: | 第101行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="bash">(tutorial-env) $ python -m pip install novas |
Collecting novas | Collecting novas | ||
Downloading novas-3.1.1.3.tar.gz (136kB) | Downloading novas-3.1.1.3.tar.gz (136kB) | ||
Installing collected packages: novas | Installing collected packages: novas | ||
Running setup.py install for novas | Running setup.py install for novas | ||
− | Successfully installed novas-3.1.1.3</ | + | Successfully installed novas-3.1.1.3</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 您还可以通过提供软件包名称后跟 <code>==</code> 和版本号来安装特定版本的软件包: | |
− | |||
<div class="highlight-bash notranslate"> | <div class="highlight-bash notranslate"> | ||
第170行: | 第117行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="bash">(tutorial-env) $ python -m pip install requests==2.6.0 |
Collecting requests==2.6.0 | Collecting requests==2.6.0 | ||
Using cached requests-2.6.0-py2.py3-none-any.whl | Using cached requests-2.6.0-py2.py3-none-any.whl | ||
Installing collected packages: requests | Installing collected packages: requests | ||
− | Successfully installed requests-2.6.0</ | + | Successfully installed requests-2.6.0</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 如果你重新运行这个命令,<code>pip</code> 会注意到请求的版本已经安装并且什么都不做。 您可以提供不同的版本号来获取该版本,或者您可以运行 <code>pip install --upgrade</code> 将软件包升级到最新版本: | |
− | |||
− | |||
<div class="highlight-bash notranslate"> | <div class="highlight-bash notranslate"> | ||
第187行: | 第132行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="bash">(tutorial-env) $ python -m pip install --upgrade requests |
Collecting requests | Collecting requests | ||
Installing collected packages: requests | Installing collected packages: requests | ||
第193行: | 第138行: | ||
Uninstalling requests-2.6.0: | Uninstalling requests-2.6.0: | ||
Successfully uninstalled requests-2.6.0 | Successfully uninstalled requests-2.6.0 | ||
− | Successfully installed requests-2.7.0</ | + | Successfully installed requests-2.7.0</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | <code>pip uninstall</code> | + | <code>pip uninstall</code> 后跟一个或多个软件包名称将从虚拟环境中删除软件包。 |
− | |||
− | <code>pip show</code> | + | <code>pip show</code> 将显示有关特定包的信息: |
<div class="highlight-bash notranslate"> | <div class="highlight-bash notranslate"> | ||
第207行: | 第151行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="bash">(tutorial-env) $ pip show requests |
--- | --- | ||
Metadata-Version: 2.0 | Metadata-Version: 2.0 | ||
第218行: | 第162行: | ||
License: Apache 2.0 | License: Apache 2.0 | ||
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages | Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages | ||
− | Requires:</ | + | Requires:</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | <code>pip list</code> | + | <code>pip list</code> 将显示所有安装在虚拟环境中的包: |
− | |||
<div class="highlight-bash notranslate"> | <div class="highlight-bash notranslate"> | ||
第230行: | 第173行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="bash">(tutorial-env) $ pip list |
novas (3.1.1.3) | novas (3.1.1.3) | ||
numpy (1.9.2) | numpy (1.9.2) | ||
pip (7.0.3) | pip (7.0.3) | ||
requests (2.7.0) | requests (2.7.0) | ||
− | setuptools (16.0)</ | + | setuptools (16.0)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | <code>pip freeze</code> | + | <code>pip freeze</code> 将生成一个类似的已安装包列表,但输出使用 <code>pip install</code> 期望的格式。 一个常见的约定是将此列表放在 <code>requirements.txt</code> 文件中: |
− | |||
− | |||
<div class="highlight-bash notranslate"> | <div class="highlight-bash notranslate"> | ||
第248行: | 第189行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="bash">(tutorial-env) $ pip freeze > requirements.txt |
(tutorial-env) $ cat requirements.txt | (tutorial-env) $ cat requirements.txt | ||
novas==3.1.1.3 | novas==3.1.1.3 | ||
numpy==1.9.2 | numpy==1.9.2 | ||
− | requests==2.7.0</ | + | requests==2.7.0</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 然后可以将 <code>requirements.txt</code> 提交给版本控制并作为应用程序的一部分提供。 然后用户可以使用 <code>install -r</code> 安装所有必要的软件包: | |
− | |||
− | |||
<div class="highlight-bash notranslate"> | <div class="highlight-bash notranslate"> | ||
第265行: | 第204行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="bash">(tutorial-env) $ python -m pip install -r requirements.txt |
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1)) | Collecting novas==3.1.1.3 (from -r requirements.txt (line 1)) | ||
... | ... | ||
第274行: | 第213行: | ||
Installing collected packages: novas, numpy, requests | Installing collected packages: novas, numpy, requests | ||
Running setup.py install for novas | Running setup.py install for novas | ||
− | Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0</ | + | Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | <code>pip</code> | + | <code>pip</code> 有更多选项。 有关 <code>pip</code> 的完整文档,请参阅 [[../../installing/index#installing-index|安装 Python 模块]] 指南。 当您编写了一个包并希望在 Python 包索引中提供它时,请参阅 [[../../distributing/index#distributing-index|分发 Python 模块]] 指南。 |
− | + | ||
− | |||
− | |||
+ | </div> | ||
</div> | </div> | ||
+ | <div class="clearer"> | ||
+ | |||
+ | |||
</div> | </div> | ||
− | [[Category:Python 3.9 | + | [[Category:Python 3.9 文档]] |
2021年10月31日 (日) 04:54的最新版本
12. 虚拟环境和包
12.1. 介绍
Python 应用程序通常会使用标准库中没有的包和模块。 应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者应用程序可能使用库接口的过时版本编写。
这意味着一个 Python 安装可能无法满足每个应用程序的要求。 如果应用程序 A 需要特定模块的 1.0 版,而应用程序 B 需要 2.0 版,则需求存在冲突,安装 1.0 或 2.0 版将导致一个应用程序无法运行。
此问题的解决方案是创建一个 虚拟环境 ,一个自包含的目录树,其中包含针对特定 Python 版本的 Python 安装,以及一些附加包。
不同的应用程序可以使用不同的虚拟环境。 为了解决前面的冲突需求示例,应用程序 A 可以拥有自己的安装了 1.0 版的虚拟环境,而应用程序 B 拥有另一个安装了 2.0 版的虚拟环境。 如果应用程序 B 需要将库升级到版本 3.0,这不会影响应用程序 A 的环境。
12.2. 创建虚拟环境
用于创建和管理虚拟环境的模块称为 venv。 venv 通常会安装您可用的最新版本的 Python。 如果您的系统上有多个 Python 版本,您可以通过运行 python3
或您想要的任何版本来选择特定的 Python 版本。
要创建虚拟环境,请确定要放置它的目录,然后使用目录路径将 venv 模块作为脚本运行:
这将创建 tutorial-env
目录(如果它不存在),并在其中创建包含 Python 解释器、标准库和各种支持文件的副本的目录。
虚拟环境的常见目录位置是 .venv
。 该名称通常将目录隐藏在您的 shell 中,从而使其不受干扰,同时为其提供一个名称来解释该目录存在的原因。 它还可以防止与某些工具支持的 .env
环境变量定义文件发生冲突。
创建虚拟环境后,您可以激活它。
在 Windows 上,运行:
在 Unix 或 MacOS 上,运行:
(此脚本是为 bash shell 编写的。 如果您使用 csh 或 fish 外壳,则应使用替代的 activate.csh
和 activate.fish
脚本。)
激活虚拟环境将更改您的 shell 提示以显示您正在使用的虚拟环境,并修改环境以便运行 python
将为您提供特定版本的 Python 和安装。 例如:
12.3. 使用 pip 管理包
您可以使用名为 pip 的程序安装、升级和删除软件包。 默认情况下pip
将从 Python Package Index 安装包,< https://pypi.org >. 您可以通过在 Web 浏览器中访问 Python 包索引来浏览它。
pip
有多个子命令:“install”、“uninstall”、“freeze”等。 (有关 pip
的完整文档,请参阅 安装 Python 模块 指南。)
您可以通过指定包的名称来安装最新版本的包:
您还可以通过提供软件包名称后跟 ==
和版本号来安装特定版本的软件包:
如果你重新运行这个命令,pip
会注意到请求的版本已经安装并且什么都不做。 您可以提供不同的版本号来获取该版本,或者您可以运行 pip install --upgrade
将软件包升级到最新版本:
pip uninstall
后跟一个或多个软件包名称将从虚拟环境中删除软件包。
pip show
将显示有关特定包的信息:
pip list
将显示所有安装在虚拟环境中的包:
pip freeze
将生成一个类似的已安装包列表,但输出使用 pip install
期望的格式。 一个常见的约定是将此列表放在 requirements.txt
文件中:
然后可以将 requirements.txt
提交给版本控制并作为应用程序的一部分提供。 然后用户可以使用 install -r
安装所有必要的软件包:
pip
有更多选项。 有关 pip
的完整文档,请参阅 安装 Python 模块 指南。 当您编写了一个包并希望在 Python 包索引中提供它时,请参阅 分发 Python 模块 指南。