遇到一台机器上部署多个不同的python程序,管理其之间import不同的libs而麻烦
http://virtualenv.openplans.org/
virtualenv通过隔离包目录和系统环境参数来实现多个相对独立的虚拟环境。
这样可避免过多的第三方库因版本依赖造成问题。
同时每个独立的虚拟环境只需通过打包即可分发,方便了系统部署。
1. 安装
[root@CT53-64-BASE ~]# pip install -U virtualenv
Downloading/unpacking virtualenv
Downloading virtualenv-1.5.1.tar.gz (1.4Mb): 1.4Mb downloaded
Running setup.py egg_info for package virtualenv
warning: no previously-included files matching '*.*' found under directory 'docs/_templates'
Installing collected packages: virtualenv
Found existing installation: virtualenv 1.5.1
Uninstalling virtualenv:
Successfully uninstalled virtualenv
Running setup.py install for virtualenv
warning: no previously-included files matching '*.*' found under directory 'docs/_templates'
Installing virtualenv script to /usr/bin
Successfully installed virtualenv
Cleaning up...
[root@CT53-64-BASE ~]#
2. 创建虚拟环境
[root@CT53-64-BASE ~]# virtualenv env_1
New python executable in env_1/bin/python
Installing setuptools............done.
[root@CT53-64-BASE ~]# ls env_1/
bin include lib lib64
[root@CT53-64-BASE ~]#
建议使用 virtualenv --no-site-packages env_1 来创建虚拟环境,限制virtualenv 继承全局的site-packages,从而达到完全虚拟环境
完全的新的虚拟python环境
[root@CT53-64-BASE env_1]# cd include/python2.4/
abstract.h codecs.h errcode.h grammar.h marshal.h objimpl.h pyconfig-64.h pymem.h setobject.h token.h
bitset.h compile.h eval.h import.h metagrammar.h opcode.h pyconfig.h pyport.h sliceobject.h traceback.h
boolobject.h complexobject.h fileobject.h intobject.h methodobject.h osdefs.h py_curses.h pystate.h stringobject.h tupleobject.h
bufferobject.h cStringIO.h floatobject.h intrcheck.h modsupport.h parsetok.h pydebug.h pystrtod.h structmember.h ucnhash.h
cellobject.h datetime.h frameobject.h iterobject.h moduleobject.h patchlevel.h pyerrors.h Python.h structseq.h unicodeobject.h
ceval.h descrobject.h funcobject.h listobject.h node.h pgen.h pyfpe.h pythonrun.h symtable.h weakrefobject.h
classobject.h dictobject.h genobject.h longintrepr.h Numeric/ pgenheaders.h pygetopt.h pythread.h sysmodule.h
cobject.h enumobject.h graminit.h longobject.h object.h pyconfig-32.h pymactoolbox.h rangeobject.h timefuncs.h
[root@CT53-64-BASE env_1]# python
Python 2.4.3 (#1, Sep 3 2009, 15:37:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for i in sys.path:
... print i
...
/usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg
/usr/lib/python2.4/site-packages/celery-2.0.3-py2.4.egg
/usr/lib/python2.4/site-packages/uuid-1.30-py2.4.egg
/usr/lib/python2.4/site-packages/multiprocessing-2.6.2.1-py2.4-linux-x86_64.egg
/usr/lib/python2.4/site-packages/pyparsing-1.5.5-py2.4.egg
/usr/lib/python2.4/site-packages/carrot-0.10.6-py2.4.egg
/usr/lib/python2.4/site-packages/anyjson-0.2.5-py2.4.egg
/usr/lib/python2.4/site-packages/SQLAlchemy-0.6.4-py2.4.egg
/usr/lib/python2.4/site-packages/python_dateutil-1.5-py2.4.egg
/usr/lib/python2.4/site-packages/importlib-1.0.2-py2.4.egg
/usr/lib/python2.4/site-packages/amqplib-0.6.1-py2.4.egg
/usr/lib/python2.4/site-packages/pip-0.8.1-py2.4.egg
/usr/lib64/python24.zip
/usr/lib64/python2.4
/usr/lib64/python2.4/plat-linux2
/usr/lib64/python2.4/lib-tk
/usr/lib64/python2.4/lib-dynload
/usr/lib64/python2.4/site-packages
/usr/lib64/python2.4/site-packages/Numeric
/usr/lib64/python2.4/site-packages/PIL
/usr/lib64/python2.4/site-packages/gst-0.10
/usr/lib64/python2.4/site-packages/gtk-2.0
/usr/lib/python2.4/site-packages
>>>
3. 激活这个虚拟python环境,其实主要是为了得到专属的虚拟site-packages
[root@CT53-64-BASE env_1]# source bin/activate
[color=red](env_1)[/color][root@CT53-64-BASE env_1]#
主要是注意shell前面的提示符变成 (env_1) 这个意味着,你再使用python的 ez_install or pip 安装第三方包到 site-packages 是你这个虚拟python环境的,不会影响到全局的系统python环境
4. 我们可以用 "deactivate" 命令退出虚拟环境
(env_1)[root@CT53-64-BASE env_1]# deactivate
[root@CT53-64-BASE env_1]#