设为首页 收藏本站
查看: 708|回复: 0

[经验分享] python virtualenv 使用

[复制链接]

尚未签到

发表于 2017-4-21 12:37:37 | 显示全部楼层 |阅读模式
Python virtualenv
[ 2010-06-02 10:14:13 | 作者: yuhen ]
字号: 大 | 中 | 小
virtualenv 的作用相当于 Sandbox,它通过隔离包目录和系统环境参数来实现多个相对独立的虚拟环境。如此可避免过多的第三方库因版本依赖造成问题。同时每个独立的虚拟环境只需通过打包即可分发,也大大方便了系统部署。
$ sudo easy_install virtualenv

现在我们可以创建虚拟环境了。
$ virtualenv test1

New python executable in test1/bin/python
Installing setuptools............done.

我们可以看到虚拟目录下已经被安装了基本所需的运行环境。
$ ls test1/bin
activate  activate_this.py  easy_install  easy_install-2.6  pip  python

$ ls test1/include/
python2.6

$ ls test1/lib
python2.6

$ ls test1/lib/python2.6/
_abcoll.py   copy_reg.pyc     linecache.py     os.pyc         sre_compile.py     stat.py
_abcoll.pyc  distutils        linecache.pyc    posixpath.py   sre_compile.pyc    stat.pyc
abc.py       encodings        locale.py        posixpath.pyc  sre_constants.py   types.py
abc.pyc      fnmatch.py       locale.pyc       re.py          sre_constants.pyc  types.pyc
codecs.py    fnmatch.pyc      ntpath.py        re.pyc         sre_parse.py       UserDict.py
codecs.pyc   genericpath.py   ntpath.pyc       site-packages  sre_parse.pyc      UserDict.pyc
config       genericpath.pyc  orig-prefix.txt  site.py        sre.py             warnings.py
copy_reg.py  lib-dynload      os.py            site.pyc       sre.pyc            warnings.pyc

进入 test1 目录,激活虚拟环境。
$ cd test1

test1$ source bin/activate

(test)test1$ which python
/home/yuhen/projects/test1/bin/python

(test)test1$ which easy_install
/home/yuhen/projects/test1/bin/easy_install

(test1)test1$ python

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import sys
>>> sys.path
['',
'/home/yuhen/projects/test1/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg',
'/home/yuhen/projects/test1/lib/python2.6/site-packages/pip-0.7.2-py2.6.egg',
'/home/yuhen/projects/test1/lib/python2.6',
'/home/yuhen/projects/test1/lib/python2.6/plat-linux2',
'/home/yuhen/projects/test1/lib/python2.6/lib-tk',
'/home/yuhen/projects/test1/lib/python2.6/lib-old',
'/home/yuhen/projects/test1/lib/python2.6/lib-dynload',
'/usr/lib/python2.6',
'/usr/lib64/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-tk',
'/home/yuhen/projects/test1/lib/python2.6/site-packages',
'/usr/local/lib/python2.6/dist-packages/virtualenv-1.4.9-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages/simplejson-2.1.1-py2.6-linux-x86_64.egg',
'/usr/local/lib/python2.6/site-packages',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/pymodules/python2.6',
'/usr/lib/pymodules/python2.6/gtk-2.0',
'/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode']
>>>

可以看到使用 "souce bin/active" 激活以后,命令提示行多了一个 "(test1)" 前缀,同时 python 和 easy_install 默认会使用虚拟环境 bin 目录下的程序。sys.path 显示当前虚拟环境的库目录被添加到搜索路径列表中。
(test1)$ easy_install MySQL-python
Searching for MySQL-python
Reading http://pypi.python.org/simple/MySQL-python/
Reading http://sourceforge.net/projects/mysql-python
Best match: MySQL-python 1.2.3c1
Downloading http://sourceforge.net/.../1.2.3c1/MySQL-python-1.2.3c1.tar.gz/download
Processing download
Running MySQL-python-1.2.3c1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-Em0wfb/MySQL-python-1.2.3c1/egg-dist-tmp-vzoJ2t
In file included from _mysql.c:36:
/usr/include/mysql/my_config.h:1050:1: warning: "HAVE_WCSCOLL" redefined
In file included from /usr/include/python2.6/Python.h:8,
                 from pymemcompat.h:10,
                 from _mysql.c:29:
/usr/include/python2.6/pyconfig.h:808:1: warning: this is the location of the previous definition
zip_safe flag not set; analyzing archive contents...
Adding MySQL-python 1.2.3c1 to easy-install.pth file

Installed /home/yuhen/projects/python/test1/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-linux-x86_64.egg
Processing dependencies for MySQL-python
Finished processing dependencies for MySQL-python

(test1)$ ls -l lib/python2.6/site-packages/
total 444
-rw-r--r-- 1 yuhen yuhen    283 2010-06-02 09:46 easy-install.pth
-rw-r--r-- 1 yuhen yuhen 106325 2010-06-02 09:46 MySQL_python-1.2.3c1-py2.6-linux-x86_64.egg
drwxr-xr-x 4 yuhen yuhen   4096 2010-06-02 09:45 pip-0.7.2-py2.6.egg
-rw-r--r-- 1 yuhen yuhen 333447 2010-06-01 23:58 setuptools-0.6c11-py2.6.egg
-rw-r--r-- 1 yuhen yuhen     30 2010-06-02 09:45 setuptools.pth

(test1)$ cat lib/python2.6/site-packages/setuptools.pth
./setuptools-0.6c11-py2.6.egg

(test1)$ python
>>> import MySQLdb
>>> MySQLdb.version_info
(1, 2, 3, 'gamma', 1)
>>>

MySQL-python 被安装到了虚拟环境中,且 easy-install.pth 中正确添加了 egg 搜索路径。

最后我们可以用 "deactivate" 命令退出虚拟环境。
(test1)test1$ deactivate

在创建虚拟环境时,我们可以添加 "--no-site-packages" 参数指示虚拟环境不要访问 global site-packages。
$ virtualenv --no-site-packages test2
New python executable in test2/bin/python
Installing setuptools............done.

$ cd test2

test2$ source bin/activate

(test2)test2$ python
>>> import sys
>>> sys.path
['',
'/home/yuhen/projects/python/test2/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg',
'/home/yuhen/projects/python/test2/lib/python2.6/site-packages/pip-0.7.2-py2.6.egg',
'/home/yuhen/projects/python/test2/lib/python2.6',
'/home/yuhen/projects/python/test2/lib/python2.6/plat-linux2',
'/home/yuhen/projects/python/test2/lib/python2.6/lib-tk',
'/home/yuhen/projects/python/test2/lib/python2.6/lib-old',
'/home/yuhen/projects/python/test2/lib/python2.6/lib-dynload',
'/usr/lib/python2.6',
'/usr/lib64/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-tk',
'/home/yuhen/projects/python/test2/lib/python2.6/site-packages']
>>>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-367425-1-1.html 上篇帖子: Python and RRD 下篇帖子: python 常用类库!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表