Python第十二课12. 虚拟环境和包

12. 虚拟环境和包

12.1. 概述?

Python应用程序通常会使用不在标准库内的软件包和模块。应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者可以使用库的过时版本的接口编写应用程序。

这意味着一个Python安装可能无法满足每个应用程序的要求。如果应用程序A需要特定模块的1.0版本但应用程序B需要2.0版本,则需求存在冲突,安装版本1.0或2.0将导致某一个应用程序无法运行。

这个问题的解决方案是创建一个 virtual environment,一个目录树,其中安装有特定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-envScriptsactivate.bat

在Unix或MacOS上,运行:

source tutorial-env/bin/activate

(这个脚本是为bash shell编写的。如果你使用 cshfish shell,你应该改用 activate.csh 或 activate.fish 脚本。)

Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using, and modify the environment so that running python will get you that particular version and installation of Python. For example:

$ source ~/envs/tutorial-env/bin/activate(tutorial-env) $ pythonPython 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> 安装软件包。你可以在浏览器中访问 Python Package Index 或是使用 pip 受限的搜索功能:

(tutorial-env) $ pip search astronomyskyfield – Elegant astronomy for Pythongary – Galactic astronomy and gravitational dynamics.novas – The United States Naval Observatory NOVAS astronomy libraryastroobs – Provides astronomy ephemeris to plan telescope observationsPyAstronomy – A collection of astronomy related tools for Python….

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

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

(tutorial-env) $ pip install novasCollecting novasDownloading novas-3.1.1.3.tar.gz (136kB)Installing collected packages: novasRunning setup.py install for novasSuccessfully installed novas-3.1.1.3

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

(tutorial-env) $ pip install requests==2.6.0Collecting requests==2.6.0Using cached requests-2.6.0-py2.py3-none-any.whlInstalling collected packages: requestsSuccessfully installed requests-2.6.0

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

(tutorial-env) $ pip install –upgrade requestsCollecting requestsInstalling collected packages: requestsFound existing installation: requests 2.6.0Uninstalling requests-2.6.0: Successfully uninstalled requests-2.6.0Successfully installed requests-2.7.0

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

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

(tutorial-env) $ pip show requests—Metadata-Version: 2.0Name: requestsVersion: 2.7.0Summary: Python HTTP for Humans.Home-page: http://python-requests.orgAuthor: Kenneth ReitzAuthor-email: me@kennethreitz.comLicense: Apache 2.0Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packagesRequires:

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

(tutorial-env) $ pip listnovas (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.txtnovas==3.1.1.3numpy==1.9.2requests==2.7.0

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

(tutorial-env) $ pip install -r requirements.txtCollecting 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, requestsRunning setup.py install for novasSuccessfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

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

声明:本站部分文章内容及图片转载于互联 、内容不代表本站观点,如有内容涉及侵权,请您立即联系本站处理,非常感谢!

(0)
上一篇 2020年3月9日
下一篇 2020年3月9日

相关推荐