1.virtualenvpython
virtualenv做用:在一個操做系統下使用不一樣的python環境,包括不一樣的python版本和不一樣的python庫包react
安裝piplinux
[root@sht-sgmhadoopcm-01 software]# wget https://bootstrap.pypa.io/get-pip.py --no-check-certificatebootstrap
[root@sht-sgmhadoopcm-01 software]# python get-pip.pyvim
安裝virtualenvbash
[root@sht-sgmhadoopcm-01 software]# pip install virtualenvapp
建立虛擬環境test1_envpython2.7
默認新建的虛擬環境包括pip、easy_install等工具ide
[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