“Python/docs/3.9/tutorial/venv”的版本间差异

来自菜鸟教程
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>
= <span class="section-number">12. </span>Virtual Environments and Packages =
+
= 12. 虚拟环境和包 =
  
 
<div id="introduction" class="section">
 
<div id="introduction" class="section">
  
== <span class="section-number">12.1. </span>Introduction ==
+
== 12.1. 介绍 ==
  
Python applications will often use packages and modules that don't
+
Python 应用程序通常会使用标准库中没有的包和模块。 应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者应用程序可能使用库接口的过时版本编写。
come as part of the standard library. Applications will sometimes
 
need a specific version of a library, because the application may
 
require that a particular bug has been fixed or the application may be
 
written using an obsolete version of the library's interface.
 
  
This means it may not be possible for one Python installation to meet
+
这意味着一个 Python 安装可能无法满足每个应用程序的要求。 如果应用程序 A 需要特定模块的 1.0 版,而应用程序 B 需要 2.0 版,则需求存在冲突,安装 1.0 2.0 版将导致一个应用程序无法运行。
the requirements of every application. If application A needs version
 
1.0 of a particular module but application B needs version 2.0, then
 
the requirements are in conflict and installing either version 1.0 or 2.0
 
will leave one application unable to run.
 
  
The solution for this problem is to create a [[../../glossary#term-virtual-environment|<span class="xref std std-term">virtual environment</span>]], a
+
此问题的解决方案是创建一个 [[../../glossary#term-virtual-environment|虚拟环境]] ,一个自包含的目录树,其中包含针对特定 Python 版本的 Python 安装,以及一些附加包。
self-contained directory tree that contains a Python installation for a
 
particular version of Python, plus a number of additional packages.
 
  
Different applications can then use different virtual environments.
+
不同的应用程序可以使用不同的虚拟环境。 为了解决前面的冲突需求示例,应用程序 A 可以拥有自己的安装了 1.0 版的虚拟环境,而应用程序 B 拥有另一个安装了 2.0 版的虚拟环境。 如果应用程序 B 需要将库升级到版本 3.0,这不会影响应用程序 A 的环境。
To resolve the earlier example of conflicting requirements,
 
application A can have its own virtual environment with version 1.0
 
installed while application B has another virtual environment with version 2.0.
 
If application B requires a library be upgraded to version 3.0, this will
 
not affect application A's environment.
 
  
  
第35行: 第21行:
 
<div id="creating-virtual-environments" class="section">
 
<div id="creating-virtual-environments" class="section">
  
== <span class="section-number">12.2. </span>Creating Virtual Environments ==
+
== 12.2. 创建虚拟环境 ==
  
The module used to create and manage virtual environments is called
+
用于创建和管理虚拟环境的模块称为 [[../../library/venv#module-venv|venv]][[../../library/venv#module-venv|venv]] 通常会安装您可用的最新版本的 Python。 如果您的系统上有多个 Python 版本,您可以通过运行 <code>python3</code> 或您想要的任何版本来选择特定的 Python 版本。
[[../../library/venv#module-venv|<code>venv</code>]]. [[../../library/venv#module-venv|<code>venv</code>]] will usually install the most recent version of
 
Python that you have available. If you have multiple versions of Python on your
 
system, you can select a specific Python version by running <code>python3</code> or
 
whichever version you want.
 
  
To create a virtual environment, decide upon a directory where you want to
+
要创建虚拟环境,请确定要放置它的目录,然后使用目录路径将 [[../../library/venv#module-venv|venv]] 模块作为脚本运行:
place it, and run the [[../../library/venv#module-venv|<code>venv</code>]] module as a script with the directory path:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第50行: 第31行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python3 -m venv tutorial-env</pre>
+
<syntaxhighlight lang="python3">python3 -m venv tutorial-env</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
This will create the <code>tutorial-env</code> directory if it doesn't exist,
+
这将创建 <code>tutorial-env</code> 目录(如果它不存在),并在其中创建包含 Python 解释器、标准库和各种支持文件的副本的目录。
and also create directories inside it containing a copy of the Python
 
interpreter, the standard library, and various supporting files.
 
  
A common directory location for a virtual environment is <code>.venv</code>.
+
虚拟环境的常见目录位置是 <code>.venv</code>。 该名称通常将目录隐藏在您的 shell 中,从而使其不受干扰,同时为其提供一个名称来解释该目录存在的原因。 它还可以防止与某些工具支持的 <code>.env</code> 环境变量定义文件发生冲突。
This name keeps the directory typically hidden in your shell and thus
 
out of the way while giving it a name that explains why the directory
 
exists. It also prevents clashing with <code>.env</code> environment variable
 
definition files that some tooling supports.
 
  
Once you've created a virtual environment, you may activate it.
+
创建虚拟环境后,您可以激活它。
  
On Windows, run:
+
Windows 上,运行:
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第73行: 第48行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>tutorial-env\Scripts\activate.bat</pre>
+
<syntaxhighlight lang="python3">tutorial-env\Scripts\activate.bat</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
On Unix or MacOS, run:
+
Unix MacOS 上,运行:
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第84行: 第59行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>source tutorial-env/bin/activate</pre>
+
<syntaxhighlight lang="python3">source tutorial-env/bin/activate</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
(This script is written for the bash shell. If you use the
+
(此脚本是为 bash shell 编写的。 如果您使用 '''csh''' '''fish''' 外壳,则应使用替代的 <code>activate.csh</code> <code>activate.fish</code> 脚本。)
'''csh''' or '''fish''' shells, there are alternate
 
<code>activate.csh</code> and <code>activate.fish</code> scripts you should use
 
instead.)
 
  
Activating the virtual environment will change your shell's prompt to show what
+
激活虚拟环境将更改您的 shell 提示以显示您正在使用的虚拟环境,并修改环境以便运行 <code>python</code> 将为您提供特定版本的 Python 和安装。 例如:
virtual environment you're using, and modify the environment so that running
 
<code>python</code> will get you that particular version and installation of Python.
 
For example:
 
  
 
<div class="highlight-bash notranslate">
 
<div class="highlight-bash notranslate">
第103行: 第72行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>$ source ~/envs/tutorial-env/bin/activate
+
<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)
 
   ...
 
   ...
&gt;&gt;&gt; import sys
+
>>> import sys
&gt;&gt;&gt; sys.path
+
>>> 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']
&gt;&gt;&gt;</pre>
+
>>></syntaxhighlight>
  
 
</div>
 
</div>
第120行: 第89行:
 
<div id="managing-packages-with-pip" class="section">
 
<div id="managing-packages-with-pip" class="section">
  
== <span class="section-number">12.3. </span>Managing Packages with pip ==
+
== 12.3. 使用 pip 管理包 ==
  
You can install, upgrade, and remove packages using a program called
+
您可以使用名为 '''pip''' 的程序安装、升级和删除软件包。 默认情况下<code>pip</code>将从 Python Package Index 安装包,&lt; https://pypi.org &gt;. 您可以通过在 Web 浏览器中访问 Python 包索引来浏览它。
'''pip'''. By default <code>pip</code> will install packages from the Python
 
Package Index, &lt;https://pypi.org&gt;. You can browse the Python
 
Package Index by going to it in your web browser, or you can use <code>pip</code>'s
 
limited search feature:
 
  
<div class="highlight-bash notranslate">
+
<code>pip</code> 有多个子命令:“install”、“uninstall”、“freeze”等。 (有关 <code>pip</code> 的完整文档,请参阅 [[../../installing/index#installing-index|安装 Python 模块]] 指南。)
 
 
<div class="highlight">
 
 
 
<pre>(tutorial-env) $ pip search astronomy
 
skyfield              - Elegant astronomy for Python
 
gary                  - Galactic astronomy and gravitational dynamics.
 
novas                  - The United States Naval Observatory NOVAS astronomy library
 
astroobs              - Provides astronomy ephemeris to plan telescope observations
 
PyAstronomy            - A collection of astronomy related tools for Python.
 
...</pre>
 
 
 
</div>
 
 
 
</div>
 
<code>pip</code> has a number of subcommands: &quot;search&quot;, &quot;install&quot;, &quot;uninstall&quot;,
 
&quot;freeze&quot;, etc. (Consult the [[../../installing/index#installing-index|<span class="std std-ref">Installing Python Modules</span>]] guide for
 
complete documentation for <code>pip</code>.)
 
  
You can install the latest version of a package by specifying a package's name:
+
您可以通过指定包的名称来安装最新版本的包:
  
 
<div class="highlight-bash notranslate">
 
<div class="highlight-bash notranslate">
第153行: 第101行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>(tutorial-env) $ python -m pip install novas
+
<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</pre>
+
Successfully installed novas-3.1.1.3</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
You can also install a specific version of a package by giving the
+
您还可以通过提供软件包名称后跟 <code>==</code> 和版本号来安装特定版本的软件包:
package name followed by <code>==</code> and the version number:
 
  
 
<div class="highlight-bash notranslate">
 
<div class="highlight-bash notranslate">
第170行: 第117行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>(tutorial-env) $ python -m pip install requests==2.6.0
+
<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</pre>
+
Successfully installed requests-2.6.0</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
If you re-run this command, <code>pip</code> will notice that the requested
+
如果你重新运行这个命令,<code>pip</code> 会注意到请求的版本已经安装并且什么都不做。 您可以提供不同的版本号来获取该版本,或者您可以运行 <code>pip install --upgrade</code> 将软件包升级到最新版本:
version is already installed and do nothing. You can supply a
 
different version number to get that version, or you can run <code>pip install --upgrade</code> to upgrade the package to the latest version:
 
  
 
<div class="highlight-bash notranslate">
 
<div class="highlight-bash notranslate">
第187行: 第132行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>(tutorial-env) $ python -m pip install --upgrade requests
+
<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</pre>
+
Successfully installed requests-2.7.0</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<code>pip uninstall</code> followed by one or more package names will remove the
+
<code>pip uninstall</code> 后跟一个或多个软件包名称将从虚拟环境中删除软件包。
packages from the virtual environment.
 
  
<code>pip show</code> will display information about a particular package:
+
<code>pip show</code> 将显示有关特定包的信息:
  
 
<div class="highlight-bash notranslate">
 
<div class="highlight-bash notranslate">
第207行: 第151行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>(tutorial-env) $ pip show requests
+
<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:</pre>
+
Requires:</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<code>pip list</code> will display all of the packages installed in the virtual
+
<code>pip list</code> 将显示所有安装在虚拟环境中的包:
environment:
 
  
 
<div class="highlight-bash notranslate">
 
<div class="highlight-bash notranslate">
第230行: 第173行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>(tutorial-env) $ pip list
+
<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)</pre>
+
setuptools (16.0)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<code>pip freeze</code> will produce a similar list of the installed packages,
+
<code>pip freeze</code> 将生成一个类似的已安装包列表,但输出使用 <code>pip install</code> 期望的格式。 一个常见的约定是将此列表放在 <code>requirements.txt</code> 文件中:
but the output uses the format that <code>pip install</code> expects.
 
A common convention is to put this list in a <code>requirements.txt</code> file:
 
  
 
<div class="highlight-bash notranslate">
 
<div class="highlight-bash notranslate">
第248行: 第189行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>(tutorial-env) $ pip freeze &gt; requirements.txt
+
<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</pre>
+
requests==2.7.0</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The <code>requirements.txt</code> can then be committed to version control and
+
然后可以将 <code>requirements.txt</code> 提交给版本控制并作为应用程序的一部分提供。 然后用户可以使用 <code>install -r</code> 安装所有必要的软件包:
shipped as part of an application. Users can then install all the
 
necessary packages with <code>install -r</code>:
 
  
 
<div class="highlight-bash notranslate">
 
<div class="highlight-bash notranslate">
第265行: 第204行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>(tutorial-env) $ python -m pip install -r requirements.txt
+
<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</pre>
+
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<code>pip</code> has many more options. Consult the [[../../installing/index#installing-index|<span class="std std-ref">Installing Python Modules</span>]]
+
<code>pip</code> 有更多选项。 有关 <code>pip</code> 的完整文档,请参阅 [[../../installing/index#installing-index|安装 Python 模块]] 指南。 当您编写了一个包并希望在 Python 包索引中提供它时,请参阅 [[../../distributing/index#distributing-index|分发 Python 模块]] 指南。
guide for complete documentation for <code>pip</code>. When you've written
+
 
a package and want to make it available on the Python Package Index,
 
consult the [[../../distributing/index#distributing-index|<span class="std std-ref">Distributing Python Modules</span>]] guide.
 
  
 +
</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. 创建虚拟环境

用于创建和管理虚拟环境的模块称为 venvvenv 通常会安装您可用的最新版本的 Python。 如果您的系统上有多个 Python 版本,您可以通过运行 python3 或您想要的任何版本来选择特定的 Python 版本。

要创建虚拟环境,请确定要放置它的目录,然后使用目录路径将 venv 模块作为脚本运行:

python3 -m venv tutorial-env

这将创建 tutorial-env 目录(如果它不存在),并在其中创建包含 Python 解释器、标准库和各种支持文件的副本的目录。

虚拟环境的常见目录位置是 .venv。 该名称通常将目录隐藏在您的 shell 中,从而使其不受干扰,同时为其提供一个名称来解释该目录存在的原因。 它还可以防止与某些工具支持的 .env 环境变量定义文件发生冲突。

创建虚拟环境后,您可以激活它。

在 Windows 上,运行:

tutorial-env\Scripts\activate.bat

在 Unix 或 MacOS 上,运行:

source tutorial-env/bin/activate

(此脚本是为 bash shell 编写的。 如果您使用 cshfish 外壳,则应使用替代的 activate.cshactivate.fish 脚本。)

激活虚拟环境将更改您的 shell 提示以显示您正在使用的虚拟环境,并修改环境以便运行 python 将为您提供特定版本的 Python 和安装。 例如:

$ source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May  6 2016, 10:59:36)
  ...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python35.zip', ...,
'~/envs/tutorial-env/lib/python3.5/site-packages']
>>>

12.3. 使用 pip 管理包

您可以使用名为 pip 的程序安装、升级和删除软件包。 默认情况下pip将从 Python Package Index 安装包,< https://pypi.org >. 您可以通过在 Web 浏览器中访问 Python 包索引来浏览它。

pip 有多个子命令:“install”、“uninstall”、“freeze”等。 (有关 pip 的完整文档,请参阅 安装 Python 模块 指南。)

您可以通过指定包的名称来安装最新版本的包:

(tutorial-env) $ python -m pip install novas
Collecting novas
  Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
  Running setup.py install for novas
Successfully installed novas-3.1.1.3

您还可以通过提供软件包名称后跟 == 和版本号来安装特定版本的软件包:

(tutorial-env) $ python -m pip install requests==2.6.0
Collecting requests==2.6.0
  Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0

如果你重新运行这个命令,pip 会注意到请求的版本已经安装并且什么都不做。 您可以提供不同的版本号来获取该版本,或者您可以运行 pip install --upgrade 将软件包升级到最新版本:

(tutorial-env) $ python -m pip install --upgrade requests
Collecting requests
Installing collected packages: requests
  Found existing installation: requests 2.6.0
    Uninstalling requests-2.6.0:
      Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0

pip uninstall 后跟一个或多个软件包名称将从虚拟环境中删除软件包。

pip show 将显示有关特定包的信息:

(tutorial-env) $ pip show requests
---
Metadata-Version: 2.0
Name: requests
Version: 2.7.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: Apache 2.0
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
Requires:

pip list 将显示所有安装在虚拟环境中的包:

(tutorial-env) $ pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)

pip freeze 将生成一个类似的已安装包列表,但输出使用 pip install 期望的格式。 一个常见的约定是将此列表放在 requirements.txt 文件中:

(tutorial-env) $ pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

然后可以将 requirements.txt 提交给版本控制并作为应用程序的一部分提供。 然后用户可以使用 install -r 安装所有必要的软件包:

(tutorial-env) $ python -m pip install -r requirements.txt
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
  ...
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
  ...
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
  ...
Installing collected packages: novas, numpy, requests
  Running setup.py install for novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

pip 有更多选项。 有关 pip 的完整文档,请参阅 安装 Python 模块 指南。 当您编写了一个包并希望在 Python 包索引中提供它时,请参阅 分发 Python 模块 指南。