|
什么是Virtualenv
Virtualenv是一個(gè)偉大的軟件,它允許創(chuàng)建一些虛擬的執(zhí)行環(huán)境。每一個(gè)都可以使用不同的python版本以及一系列不同的庫。
在本教程中我主要使用了以下的工具和版本:
Virtualenv – 1.4.8 Python – 2.6.5 Ubuntu 10.04 64 bit All console programs run with UTF-8
我需要虛擬環(huán)境么?
通常這要看實(shí)際情況,一般情況下你可以不使用它來創(chuàng)建和使用python程序,但是使用virtualenv會(huì)有很大幫助。
這些虛擬環(huán)境提供了非常多的可能性:
在生產(chǎn)服務(wù)器上允許運(yùn)行不同python版本的應(yīng)用。
在測(cè)試服務(wù)器上允許執(zhí)行很多測(cè)試包括: 1. 測(cè)試安裝腳本看其是否真正地安裝了所有必需的庫并且檢查相應(yīng)的版本。 2. 使用不同版本的庫對(duì)應(yīng)用進(jìn)行測(cè)試。 3. 測(cè)試升級(jí)一個(gè)庫版本不會(huì)導(dǎo)致程序崩潰。
Virtualenv的基本步驟
安裝
Virtualenv的安裝非常簡(jiǎn)單,最好的方式就是在全局范圍內(nèi)進(jìn)行安裝以便于所有人都能夠創(chuàng)建和使用virtualenv。因此你最好是以管理員的身份來安裝。我在筆記本上使用的Ubuntu系統(tǒng),因此我將以sudo命令來安裝,在其他系統(tǒng)上也是類似的操作。下面是安裝命令:
sudo easy_install virtualenv
如果命令成功執(zhí)行,會(huì)在終端中打印如下信息:
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
創(chuàng)建第一個(gè)虛擬環(huán)境
創(chuàng)建虛擬環(huán)境也非常簡(jiǎn)單。首先選擇好想創(chuàng)建的目錄,最好的方式是在你的家目錄中執(zhí)行。
讓我們創(chuàng)建一個(gè)特殊的目錄來存放所有不同的環(huán)境。該目錄的名字叫virt_env。使用如下命令來創(chuàng)建:
mkdir virt_env
現(xiàn)在在該目錄中創(chuàng)建第一個(gè)虛擬環(huán)境,正常情況下使用如下命令完成:
virtualenv virt_env/virt1
首先我們來看一下該命令接受的最有用的一些參數(shù)。
Virtualenv參數(shù)
--help或-h——unix中獲取應(yīng)用信息的標(biāo)準(zhǔn)參數(shù)。我至今還沒發(fā)現(xiàn)任何不使用此參數(shù)的應(yīng)用程式,因而比較好記。 --verbose或-v——通常程序會(huì)打出很多信息,這些信息可以幫助解決一些問題。 --quiet或-q——與—versbose參數(shù)相反,程序會(huì)打出非常少的信息出來。 --clear——如果你想要重新安裝virtualenv,使用這個(gè)參數(shù)可以刪除掉之前的安裝文件,并安裝一個(gè)新的,因而達(dá)到重裝的效果。 --version——打印出應(yīng)用的版本號(hào),對(duì)于我來說打印出1.4.8。 --no-site-packages——所有通過easy_install命令安裝的包都是全局的,所有的用戶都能夠訪問并使用它們。一些時(shí)候這是有用的,但是我想要一個(gè)純凈的虛擬環(huán)境而不管在系統(tǒng)中安裝什么程序或者在其他虛擬環(huán)境中安裝什么。使用這個(gè)參數(shù)就能達(dá)到目的,新環(huán)境不會(huì)使用任何系統(tǒng)的python包。
如何創(chuàng)建Python的虛擬環(huán)境
我現(xiàn)在會(huì)改變之前使用的創(chuàng)建命令,添加一些參數(shù)。因?yàn)椴幌胧褂萌魏尾僮飨到y(tǒng)預(yù)裝的包,因而會(huì)用如下命令:
virtualenv virt_env/virt1 --no-site-packages
該命令的輸出如下:
New python executable in virt_env/virt1/bin/python Installing setuptools............done.
如果使用了—verbose參數(shù),那么輸出信息會(huì)有點(diǎn)混亂,所以一般情況下不要使用它。只有在調(diào)試一些錯(cuò)誤時(shí)或者你想知道它是如何工作時(shí)再使用它:
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的虛擬環(huán)境
激活環(huán)境
使用虛擬環(huán)境比較簡(jiǎn)單,首先去定義你要使用的環(huán)境。目前為止只有一個(gè)環(huán)境,之后可能會(huì)多些。
現(xiàn)在使用這個(gè)環(huán)境,環(huán)境的目錄為virt_env/virt1/,下面的命令可以加載這個(gè)環(huán)境:
virt@ymon:~$ source virt_env/virt1/bin/activate
命令成功執(zhí)行后會(huì)改變提示語句,現(xiàn)在看起來如下:
(virt1)virt@ymon:~$
提示語句的第一部分就是虛擬環(huán)境的名字,因此很容易知道當(dāng)前所處的是哪個(gè)環(huán)境。當(dāng)然你也可以將提示語句改成之前的樣式,但是我不建議你這么做。因?yàn)楫?dāng)你使用多個(gè)環(huán)境時(shí),這會(huì)非常混亂。
退出當(dāng)前環(huán)境
可以使用如下命令退出環(huán)境:
(virt1)virt@ymon:~$ deactivate
提示語句也會(huì)變?yōu)橹暗臉幼樱缦滤荆?br>
virt@ymon:~$
deactivate命令只有當(dāng)虛擬環(huán)境被激活時(shí)有用,所以當(dāng)你在環(huán)境之外執(zhí)行時(shí)并不 會(huì)產(chǎn)生任何效果。
在虛擬環(huán)境中安裝包
檢查已經(jīng)安裝的包
為了查看已經(jīng)安裝的包,我使用yolk命令。這是一個(gè)小的終端程序可以列舉出所有安裝的包。
安裝比較簡(jiǎn)單:sudo easy_install yolk,而使用更加簡(jiǎn)單:yolk –l。
在虛擬環(huán)境之外使用那個(gè)命令會(huì)告訴我已經(jīng)安裝了114包,所以我并不會(huì)貼在這里。
在新的環(huán)境中使用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:~$
現(xiàn)在可以在本地中使用它了,在新的環(huán)境中沒有那么多包:
(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
安裝其他包
現(xiàn)在我可以正常地安裝所有的包,只在虛擬環(huán)境中。它不會(huì)觸及到環(huán)境之外的任何包,甚至那些全局的包。
比如說在環(huán)境中安裝Pylons,首先創(chuàng)建另一個(gè)虛擬環(huán)境以確保真的只在一個(gè)環(huán)境中被安裝:
(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.
現(xiàn)在有了另一個(gè)環(huán)境,我們來檢查下安裝的包都有哪些:
(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
很好,跟之前的完全一致。現(xiàn)在我切換到第一個(gè)環(huán)境中安裝Pylons。
(virt2)virt@ymon:~$ deactivate virt@ymon:~$ source virt_env/virt1/bin/activate (virt1)virt@ymon:~$ easy_install Pylons
該命令會(huì)在終端中打印出非常多的信息因?yàn)镻ylons依賴很多其他的包。最后的幾行信息如下所示:
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:~$
再安裝一個(gè)SQLAlchemy庫:
(virt1)virt@ymon:~$ easy_install SqlAlchemy
現(xiàn)在再來檢查下安裝的包列表:
(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?,F(xiàn)在再來看下第二個(gè)虛擬環(huán)境,是否包含了這些安裝的包呢?
(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
看起來所有的事都在預(yù)料之中,現(xiàn)在再安裝一個(gè)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的信息。我們現(xiàn)在安裝一個(gè)之前版本的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
看起來不錯(cuò),再來檢查一下:
(virt2)virt@ymon:~$ yolk -l | grep SQLAlchemy SQLAlchemy - 0.5.0 - active
總結(jié)
現(xiàn)在我擁有了兩個(gè)虛擬環(huán)境:
virt_env/virt1/ virt_env/virt2/
在不同的環(huán)境中安裝了不同的SQLAlchemy版本:
In the first there is SQLAlchemy 0.6.0 In the second there is SQLAlchemy 0.5.0.
因此現(xiàn)在可以很容易地測(cè)試在配置文件不變情況下,Pylons應(yīng)用在不同的SQLAlchemy版本上的行為是否有所不同。
英文原文:http://www./virtualenv-tutorial/ 譯者:angelo
|