[译]The Python Tutorial#12. Virtual Environments and Packages
[译]The Python Tutorial#Virtual Environments and Packages
12.1 Introduction
Python应用经常使用不属于标准库的包和模块。应用有时会需要一个特定版本的库,因为应用可能会需要一个特定bug得到修复的库,或者应用依赖库中一个废弃版本的接口。
这意味着一个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 Creating Virtual Environments
用来创建和管理虚拟换环境模块叫做venv。venv通常会安装最近版本的Python。如果系统中有多个版本的Python,可以运行python3或者其他任何版本来选择一个指定的Python版本。
要创建一个虚拟环境,首先需要决定放置虚拟环境的目录,然后以脚本方式运行带有目录路径的venv模块:
python3 -m venv tutorial-env
如果tutorial-env目录不存在,会创建一个tutorial-env目录,同时在目录中创建子目录,包含Python解释器,标准库以及各种各样支持文件的副本。
一旦创建了一个虚拟环境,就可以激活它了。
在Windows中,运行:
tutorial-env\Scripts\activate.bat
在Unix或者MacOS中,运行:
source tutorial-env/bin/activate
(这个脚本使用bash shell编写。如果你使用csh或者fish shell,有activae.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 Managing Packages with pip
使用一个叫做pip的程序,可以安装,更新和移除包。默认情况下pip会从Python Package Index https://pypi.python.org/pypi 安装包。可以使用浏览器浏览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有一系列子命令:"search", "install", "uninstall", "freeze"等等(参考 Installing Python Modules获取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还有其他许多选项。查阅 Installing Python Modules 获取完整的pip文档。如果开发了一个包,并且希望在 Python Package Index上可用,查阅 Distributing Python Modules。
[译]The Python Tutorial#12. Virtual Environments and Packages的更多相关文章
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
- [译]The Python Tutorial#7. Input and Output
[译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...
- [译]The Python Tutorial#5. Data Structures
[译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...
- [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II
[译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...
- [译]The Python Tutorial#2. Using the Python Interpreter
[译]The Python Tutorial#Using the Python Interpreter 2.1 Invoking the Interpreter Python解释器通常安装在目标机器的 ...
- [译]The Python Tutorial#1. Whetting Your Appetite
[译]The Python Tutorial#Whetting Your Appetite 1. Whetting Your Appetite 如果你需要使用计算机做很多工作,最终会发现很多任务需要自 ...
- [译]The Python Tutorial#8. Errors and Exceptions
[译]The Python Tutorial#Errors and Exceptions 到现在为止都没有过多介绍错误信息,但是已经在一些示例中使用过错误信息.Python至少有两种类型的错误:语法错 ...
- [译]The Python Tutorial#4. More Control Flow Tools
[译]The Python Tutorial#More Control Flow Tools 除了刚才介绍的while语句之外,Python也从其他语言借鉴了其他流程控制语句,并做了相应改变. 4.1 ...
- [译]The Python Tutorial#6. Modules
[译]The Python Tutorial#Modules 6. Modules 如果你从Python解释器中退出然后重新进入,之前定义的名字(函数和变量)都丢失了.因此,如果你想写长一点的程序,使 ...
随机推荐
- Linux Bash 命令行快捷键小结
编辑命令 光标移动 剪切.删除 Ctrl ...
- Hive 基本语法操练(五):Hive 的 JOIN 用法
Hive 的 JOIN 用法 hive只支持等连接,外连接,左半连接.hive不支持非相等的join条件(通过其他方式实现,如left outer join),因为它很难在map/reduce中实现这 ...
- 解析和操作XML文件
Dom4j工具 使用步骤: 1)导入dom4j的核心包. dom4j-1.6.1.jar 2)编写Dom4j读取xml文件代码 1,Domj4读取xml文件 ,准备工作:读取整个文档并获取根节点 // ...
- android 开发-HttpClient状态码定义
TP 定义的状态代码的值(.net HttpWebResponse.HttpStatusCode 成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续 ...
- agc016C - +/- Rectangle(构造 智商题)
题意 题目链接 Sol 我的思路:直接按样例一的方法构造,若$h \times w$完全被$N \times M$包含显然无解 emm,wa了一发之后发现有反例:1 4 1 3 我的会输出[1 1 - ...
- Android 使用greenDAO 3.2.2 操作外部数据库
项目开发中有时需要用到一些写死的数据,如公司的产品信息之类的.这就需要我们先把数据库文件保存在资源文件夹下,然后当应用创建时将数据库文件拷到应用安装目录的/databases/文件夹下,然后再对数据进 ...
- SpringBoot的优缺点
优点: 1.快速构建项目 2.对主流开发框架的无配置继承 3.项目可独立运行,无须外部依赖Servlet容器 4.提高运行时的应用监控 5.极大地提高了开发.部署效率 6.与云计算的天然集成 缺点: ...
- silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
asp.net开发中,数据绑定是一个很简单的概念,控件与数据绑定后,控件可以自动把数据按一定的形式显示出来.(当然控件上的值改变后,可以通过提交页面表单,同时后台服务端代码接收新值更新数据) silv ...
- 报错:无法打开"cocos-ext.h" /添加第三方库
参考原文:http://lin-jianlong.diandian.com/post/2012-11-05/40042951271 1.项目属性->配置属性->C/C++->常规-& ...
- pysnmp程序
功能 访问远程交换机snmp数据,写入本地influxdb数据库 #!/usr/bin/env python # -*- encoding: utf-8 -*- import os, yaml, ti ...