标签:virtualenv 01 sht python 虚拟环境 env sgmhadoopcm root software
1.virtualenv
virtualenv作用:在一个操作系统下使用不同的python环境,包括不同的python版本和不同的python库包
安装pip
[root@sht-sgmhadoopcm-01 software]# wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
[root@sht-sgmhadoopcm-01 software]# python get-pip.py
安装virtualenv
[root@sht-sgmhadoopcm-01 software]# pip install virtualenv
创建虚拟环境test1_env
默认新建的虚拟环境包括pip、easy_install等工具
[root@sht-sgmhadoopcm-01 software]# virtualenv test1_env
New python executable in /opt/software/test1_env/bin/python
Installing setuptools, pip, wheel...
done.
会生成响应的目录和文件
[root@sht-sgmhadoopcm-01 software]# ls -lh test1_env/
total 0
drwxr-xr-x 2 root root 268 Feb 12 17:30 bin
drwxr-xr-x 3 root root 23 Feb 12 17:30 include
drwxr-xr-x 3 root root 23 Feb 12 17:30 lib
lrwxrwxrwx 1 root root 3 Feb 12 17:30 lib64 -> lib
启用虚拟环境
这个环境下执行python相关的操作都是限定在虚拟环境中
[root@sht-sgmhadoopcm-01 software]# source test1_env/bin/activate
(test1_env) [root@sht-sgmhadoopcm-01 software]#
查看当前虚拟环境的python目录是否正确
>>> import sys
>>> print sys.path
['', '/opt/software/test1_env/lib64/python27.zip', '/opt/software/test1_env/lib64/python2.7', '/opt/software/test1_env/lib64/python2.7/plat-linux2', '/opt/software/test1_env/lib64/python2.7/lib-tk', '/opt/software/test1_env/lib64/python2.7/lib-old', '/opt/software/test1_env/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7', '/usr/lib/python2.7', '/opt/software/test1_env/lib/python2.7/site-packages']
退出虚拟环境
(test1_env) [root@sht-sgmhadoopcm-01 bin]# deactivate
测试1:在系统环境中安装第三方库,虚拟环境没有影响
[root@sht-sgmhadoopcm-01 software]# pip install jinja2
[root@sht-sgmhadoopcm-01 software]# pip list|grep Jinja2
Jinja2
[root@sht-sgmhadoopcm-01 software]# source test1_env/bin/activate
(test1_env) [root@sht-sgmhadoopcm-01 software]# pip list|grep jinja2|wc -l
0
测试2:在虚拟环境中安装第三方库,系统环境没有影响
[root@sht-sgmhadoopcm-01 software]# source test1_env/bin/activate
(test1_env) [root@sht-sgmhadoopcm-01 software]# pip install futures
(test1_env) [root@sht-sgmhadoopcm-01 software]# pip list|grep futures
futures 3.2.0
[root@sht-sgmhadoopcm-01 software]# pip list |grep futures|wc -l
0
2.virtualenvwrapper
virtualenvwrapper作用:集中管理虚拟环境
安装virtualenvwrapper
[root@sht-sgmhadoopcm-01 software]# pip install virtualenvwrapper
[root@sht-sgmhadoopcm-01 ~]# vim ~/.bash_profile
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
source /bin/virtualenvwrapper.sh
[root@sht-sgmhadoopcm-01 ~]# source ~/.bash_profile
新建虚拟环境:mkvirtualenv [虚拟环境名称]
[root@sht-sgmhadoopcm-01 software]# mkvirtualenv test3_env
New python executable in /root/.virtualenvs/test3_env/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test3_env/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test3_env/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test3_env/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test3_env/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test3_env/bin/get_env_details
[root@sht-sgmhadoopcm-01 software]# mkvirtualenv test4_env
列出虚拟环境列表:workon或者lsvirtualenv
[root@sht-sgmhadoopcm-01 .virtualenvs]# workon
test3_env
test4_env
启动/切换虚拟环境:workon [虚拟环境名称]
[root@sht-sgmhadoopcm-01 .virtualenvs]# workon test3_env
(test3_env) [root@sht-sgmhadoopcm-01 .virtualenvs]# workon test4_env
删除虚拟环境:rmvirtualenv [虚拟环境名称]
(test4_env) [root@sht-sgmhadoopcm-01 .virtualenvs]# rmvirtualenv test3_env
标签:virtualenv,01,sht,python,虚拟环境,env,sgmhadoopcm,root,software 来源: http://blog.51cto.com/darrenmemos/2349552
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。