各种操作系统中venv虚拟环境创建、激活和pip包安装与升级更新(12)python入门教程(原版)
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
模块作为带有目录路径的脚本运行:
python3 -m venv tutorial-env
tutorial-env
如果目录不存在,这将创建目录,并在其中创建包含Python解释器,标准库和各种支持文件的副本的目录。
创建虚拟环境后,您可以激活它。
在Windows上,运行:
tutorial-env\Scripts\activate.bat
在Unix或MacOS上,运行:
source tutorial-env/bin/activate
(这个脚本是为bash shell编写的。如果你使用 csh或fish shell,你应该使用替代 activate.csh
和activate.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包索引< https://pypi.org > 安装包。您可以在Web浏览器中浏览Python Package Index,也可以使用pip
有限的搜索功能:
(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.
...
pip
有许多子命令:“搜索”,“安装”,“卸载”,“冻结”等。(有关完整文档,请参阅安装Python模块指南pip
。)
您可以通过指定包的名称来安装最新版本的包:
(tutorial-env) $ 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) $ 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) $ 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) $ 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
有更多的选择。有关 完整文档,请参阅安装Python模块指南pip
。当您编写包并希望在Python包索引中使其可用时,请参阅Distributing Python Modules指南。