什么是Virtualenv
Virtualenv是一个伟大的软件,它允许创建一些虚拟的执行环境。每一个都可以使用不同的python版本以及一系列不同的库。
在本教程中我主要使用了以下的工具和版本:
Virtualenv – 1.4.8 Python – 2.6.5 Ubuntu 10.04 64 bit All console programs run with UTF-8
我需要虚拟环境么?
通常这要看实际情况,一般情况下你可以不使用它来创建和使用python程序,但是使用virtualenv会有很大帮助。
这些虚拟环境提供了非常多的可能性:
Virtualenv的基本步骤
安装
Virtualenv的安装非常简单,最好的方式就是在全局范围内进行安装以便于所有人都能够创建和使用virtualenv。因此你最好是以管理员的身份来安装。我在笔记本上使用的Ubuntu系统,因此我将以sudo命令来安装,在其他系统上也是类似的操作。下面是安装命令:
sudo easy_install virtualenv
如果命令成功执行,会在终端中打印如下信息:
sudo easy_install virtualenv Searching for virtualenv Best match: virtualenv 1.4.8 Processing virtualenv-1.4.8-py2.6.egg Adding virtualenv 1.4.8 to easy-install.pth file Installing virtualenv script to /usr/local/bin Using /usr/local/lib/python2.6/dist-packages/virtualenv-1.4.8-py2.6.egg Processing dependencies for virtualenv Finished processing dependencies for virtualenv
创建第一个虚拟环境
创建虚拟环境也非常简单。首先选择好想创建的目录,最好的方式是在你的家目录中执行。
让我们创建一个特殊的目录来存放所有不同的环境。该目录的名字叫virt_env。使用如下命令来创建:
mkdir virt_env
现在在该目录中创建第一个虚拟环境,正常情况下使用如下命令完成:
virtualenv virt_env/virt1
首先我们来看一下该命令接受的最有用的一些参数。
Virtualenv参数
--help或-h——unix中获取应用信息的标准参数。我至今还没发现任何不使用此参数的应用程式,因而比较好记。 --verbose或-v——通常程序会打出很多信息,这些信息可以帮助解决一些问题。 --quiet或-q——与—versbose参数相反,程序会打出非常少的信息出来。 --clear——如果你想要重新安装virtualenv,使用这个参数可以删除掉之前的安装文件,并安装一个新的,因而达到重装的效果。 --version——打印出应用的版本号,对于我来说打印出1.4.8。 --no-site-packages——所有通过easy_install命令安装的包都是全局的,所有的用户都能够访问并使用它们。一些时候这是有用的,但是我想要一个纯净的虚拟环境而不管在系统中安装什么程序或者在其他虚拟环境中安装什么。使用这个参数就能达到目的,新环境不会使用任何系统的python包。
如何创建Python的虚拟环境
我现在会改变之前使用的创建命令,添加一些参数。因为不想使用任何操作系统预装的包,因而会用如下命令:
virtualenv virt_env/virt1 --no-site-packages
该命令的输出如下:
New python executable in virt_env/virt1/bin/python Installing setuptools............done.
如果使用了—verbose参数,那么输出信息会有点混乱,所以一般情况下不要使用它。只有在调试一些错误时或者你想知道它是如何工作时再使用它:
virtualenv virt_env/virt1 --no-site-packages --verbose
Creating virt_env/virt1/lib/python2.6 Symlinking Python bootstrap modules Symlinking virt_env/virt1/lib/python2.6/re.pyc Symlinking virt_env/virt1/lib/python2.6/codecs.pyc 。。。。。。这里省略了一部分输出 Creating virt_env/virt1/lib/python2.6/site-packages Writing virt_env/virt1/lib/python2.6/site.py Writing virt_env/virt1/lib/python2.6/orig-prefix.txt Writing virt_env/virt1/lib/python2.6/no-global-site-packages.txt Creating parent directories for virt_env/virt1/include Symlinking virt_env/virt1/include/python2.6 Creating virt_env/virt1/bin New python executable in virt_env/virt1/bin/python Changed mode of virt_env/virt1/bin/python to 0755 Testing executable with virt_env/virt1/bin/python -c 'import sys; print sys.prefix' Got sys.prefix result: '/home/virt/virt_env/virt1' Creating virt_env/virt1/lib/python2.6/distutils Writing virt_env/virt1/lib/python2.6/distutils/__init__.py Writing virt_env/virt1/lib/python2.6/distutils/distutils.cfg Using existing setuptools egg: /usr/local/lib/python2.6/dist-packages/virtualenv-1.4.8-py2.6.egg/virtualenv_support/setuptools-0.6c11-py2.6.egg Installing setuptools.......... Processing dependencies for setuptools==0.6c11 Finished processing dependencies for setuptools==0.6c11 ...Installing setuptools...done. Installing pip-0.7.1.tar.gz Processing pip-0.7.1.tar.gz Running pip-0.7.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-xgmRQJ/pip-0.7.1/egg-dist-tmp-N5lGfG warning: no previously-included files matching '*.txt' found under directory 'docs/_build' no previously-included directories found matching 'docs/_build/_sources' zip_safe flag not set; analyzing archive contents... pip.venv: module references __file__ pip.basecommand: module references __file__ pip.runner: module references __file__ pip.vcs.__init__: module references __file__ Adding pip 0.7.1 to easy-install.pth file Processing dependencies for pip==0.7.1 Finished processing dependencies for pip==0.7.1 Writing virt_env/virt1/bin/activate Writing virt_env/virt1/bin/activate_this.py
使用Python的虚拟环境
激活环境
使用虚拟环境比较简单,首先去定义你要使用的环境。目前为止只有一个环境,之后可能会多些。
现在使用这个环境,环境的目录为virt_env/virt1/,下面的命令可以加载这个环境:
virt@ymon:~$ source virt_env/virt1/bin/activate
命令成功执行后会改变提示语句,现在看起来如下:
(virt1)virt@ymon:~$
提示语句的第一部分就是虚拟环境的名字,因此很容易知道当前所处的是哪个环境。当然你也可以将提示语句改成之前的样式,但是我不建议你这么做。因为当你使用多个环境时,这会非常混乱。
退出当前环境
可以使用如下命令退出环境:
(virt1)virt@ymon:~$ deactivate
提示语句也会变为之前的样子,如下所示:
virt@ymon:~$
deactivate命令只有当虚拟环境被激活时有用,所以当你在环境之外执行时并不 会产生任何效果。
在虚拟环境中安装包
检查已经安装的包
为了查看已经安装的包,我使用yolk命令。这是一个小的终端程序可以列举出所有安装的包。
安装比较简单:sudo easy_install yolk,而使用更加简单:yolk –l。
在虚拟环境之外使用那个命令会告诉我已经安装了114包,所以我并不会贴在这里。
在新的环境中使用yolk命令需要在本地安装,命令如下:
virt@ymon:~$ source virt_env/virt1/bin/activate (virt1)virt@ymon:~$ easy_install yolk Searching for yolk Best match: yolk 0.4.1 Processing yolk-0.4.1-py2.6.egg yolk 0.4.1 is already the active version in easy-install.pth Installing yolk script to /home/virt/virt_env/virt1/bin
Using /home/virt/virt_env/virt1/lib/python2.6/site-packages/yolk-0.4.1-py2.6.egg Processing dependencies for yolk Finished processing dependencies for yolk (virt1)virt@ymon:~$
现在可以在本地中使用它了,在新的环境中没有那么多包:
(virt1)virt@ymon:~$ yolk -l Python - 2.6.5 - active development (/usr/lib/python2.6/lib-dynload) pip - 0.7.1 - active setuptools - 0.6c11 - active wsgiref - 0.1.2 - active development (/usr/lib/python2.6) yolk - 0.4.1 - active
安装其他包
现在我可以正常地安装所有的包,只在虚拟环境中。它不会触及到环境之外的任何包,甚至那些全局的包。
比如说在环境中安装Pylons,首先创建另一个虚拟环境以确保真的只在一个环境中被安装:
(virt1)virt@ymon:~$ deactivate virt@ymon:~$ virtualenv virt_env/virt2 --no-site-packages New python executable in virt_env/virt2/bin/python Installing setuptools............done.
现在有了另一个环境,我们来检查下安装的包都有哪些:
(virt2)virt@ymon:~$ yolk -l Python - 2.6.5 - active development (/usr/lib/python2.6/lib-dynload) pip - 0.7.1 - active setuptools - 0.6c11 - active wsgiref - 0.1.2 - active development (/usr/lib/python2.6) yolk - 0.4.1 - active
很好,跟之前的完全一致。现在我切换到第一个环境中安装Pylons。
(virt2)virt@ymon:~$ deactivate virt@ymon:~$ source virt_env/virt1/bin/activate (virt1)virt@ymon:~$ easy_install Pylons
该命令会在终端中打印出非常多的信息因为Pylons依赖很多其他的包。最后的几行信息如下所示:
Installed /home/virt/virt_env/virt1/lib/python2.6/site-packages/Pygments-1.3.1-py2.6.egg Finished processing dependencies for Pylons (virt1)virt@ymon:~$
再安装一个SQLAlchemy库:
(virt1)virt@ymon:~$ easy_install SqlAlchemy
现在再来检查下安装的包列表:
(virt1)virt@ymon:~$ yolk -l Beaker - 1.5.3 - active FormEncode - 1.2.2 - active Mako - 0.3.2 - active Paste - 1.7.3.1 - active PasteDeploy - 1.3.3 - active PasteScript - 1.7.3 - active Pygments - 1.3.1 - active Pylons - 0.10rc1 - active Python - 2.6.5 - active development (/usr/lib/python2.6/lib-dynload) Routes - 1.12.1 - active SQLAlchemy - 0.6.0 - active Tempita - 0.4 - active WebError - 0.10.2 - active WebHelpers - 1.0b6 - active WebOb - 0.9.8 - active WebTest - 1.2.1 - active decorator - 3.1.2 - active nose - 0.11.3 - active pip - 0.7.1 - active setuptools - 0.6c11 - active simplejson - 2.1.1 - active wsgiref - 0.1.2 - active development (/usr/lib/python2.6) yolk - 0.4.1 - active
可以看到SQLAlchemy库的版本是0.6.0。现在再来看下第二个虚拟环境,是否包含了这些安装的包呢?
(virt1)virt@ymon:~$ deactivate virt@ymon:~$ source virt_env/virt2/bin/activate (virt2)virt@ymon:~$ yolk -l Python - 2.6.5 - active development (/usr/lib/python2.6/lib-dynload) pip - 0.7.1 - active setuptools - 0.6c11 - active wsgiref - 0.1.2 - active development (/usr/lib/python2.6) yolk - 0.4.1 - active
看起来所有的事都在预料之中,现在再安装一个Pylons,然后检查下安装的包列表:
(virt1)virt@ymon:~$ deactivate virt@ymon:~$ source virt_env/virt2/bin/activate (virt2)virt@ymon:~$ easy_install Pylons [... here a lot of messages ...] Processing dependencies for Pylons Finished processing dependencies for Pylons (virt2)virt@ymon:~$ yolk -l Beaker - 1.5.3 - active FormEncode - 1.2.2 - active Mako - 0.3.2 - active Paste - 1.7.3.1 - active PasteDeploy - 1.3.3 - active PasteScript - 1.7.3 - active Pygments - 1.3.1 - active Pylons - 0.10rc1 - active Python - 2.6.5 - active development (/usr/lib/python2.6/lib-dynload) Routes - 1.12.1 - active Tempita - 0.4 - active WebError - 0.10.2 - active WebHelpers - 1.0b6 - active WebOb - 0.9.8 - active WebTest - 1.2.1 - active decorator - 3.1.2 - active nose - 0.11.3 - active pip - 0.7.1 - active setuptools - 0.6c11 - active simplejson - 2.1.1 - active wsgiref - 0.1.2 - active development (/usr/lib/python2.6) yolk - 0.4.1 - active
非常好,并不包含任何SQLAlchemy的信息。我们现在安装一个之前版本的SQLAlchemy,比如说0.5.0.
(virt2)virt@ymon:~$ easy_install 'SQLAlchemy==0.5.0' Searching for SQLAlchemy==0.5.0 Reading http://pypi./simple/SQLAlchemy/ Reading http://www. Best match: SQLAlchemy 0.5.0 Downloading http://pypi./packages/source/S/SQLAlchemy/SQLAlchemy-0.5.0.tar.gz##md5=df49f403b2db3c54aace64aebe26cf90 Processing SQLAlchemy-0.5.0.tar.gz Running SQLAlchemy-0.5.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-T1eI2f/SQLAlchemy-0.5.0/egg-dist-tmp-jnaNHq no previously-included directories found matching 'doc/build/output' zip_safe flag not set; analyzing archive contents... sqlalchemy.databases.mysql: module MAY be using inspect.stack Adding SQLAlchemy 0.5.0 to easy-install.pth file
Installed /home/virt/virt_env/virt2/lib/python2.6/site-packages/SQLAlchemy-0.5.0-py2.6.egg Processing dependencies for SQLAlchemy==0.5.0 Finished processing dependencies for SQLAlchemy==0.5.0
看起来不错,再来检查一下:
(virt2)virt@ymon:~$ yolk -l | grep SQLAlchemy SQLAlchemy - 0.5.0 - active
总结
现在我拥有了两个虚拟环境:
virt_env/virt1/ virt_env/virt2/
在不同的环境中安装了不同的SQLAlchemy版本:
In the first there is SQLAlchemy 0.6.0 In the second there is SQLAlchemy 0.5.0.
因此现在可以很容易地测试在配置文件不变情况下,Pylons应用在不同的SQLAlchemy版本上的行为是否有所不同。
英文原文:http://www./virtualenv-tutorial/ 译者:angelo
|