pip install virtualenv
virtualenv venv
virtualenv -p /usr/bin/python2.7 venv
在默認狀況下,全部安裝在系統範圍內的包對於virtualenv是可見的。 這意味着若是你將simplejson安裝在您的系統Python目錄中,它會自動提供給全部的virtualenvs使用。 這種行爲能夠被更改,在建立virtualenv時增長 --no-site-packages 選項的virtualenv就不會讀取系統包python
virtualenv venv --no-site-packages
source venv/bin/activate
deactivate
因爲virtualenv用起來有點麻煩,wrapper對它進行了封裝,讓它更好用,最終咱們使用wrapper提供的命令,可是實際工做都是virtualenv作的react
使用管理員權限執行linux
# pip install virtualenv virtualenvwrapper
if [ -f /usr/local/python/bin/virtualenvwrapper.sh ]; then #虛擬環境的總倉庫 export WORKON_HOME=$HOME/VenvRepo #指定python的版本 export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.5 #執行腳本,不一樣安裝的python的路徑有能夠不一樣,能夠使用which python查看 source /usr/local/python/bin/virtualenvwrapper.sh fi
而後將下面的命令運行一次:json
linux環境執行:centos
source ~/.bashrc
執行成功會有以下輸出[centos環境爲例]:bash
[liuyuantao@localhost ~]$ source ~/.bashrc virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/premkproject virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/postmkproject virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/initialize virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/premkvirtualenv virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/postmkvirtualenv virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/prermvirtualenv virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/postrmvirtualenv virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/predeactivate virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/postdeactivate virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/preactivate virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/postactivate virtualenvwrapper.user_scripts creating /home/liuyuantao/Develop/venv-repo/get_env_details [liuyuantao@localhost ~]$
mkvirtualenv nowamagic_venv #新建虛擬空間 workon nowamagic_venv #激活並使用虛擬空間 deactivate #退出虛擬空間 rmvirtualenv nowamagic_venv #刪除虛擬空間
workon 切換到環境 deactivate 註銷當前環境 lsvirtualenv 列出全部環境 rmvirtualenv 刪除環境 cpvirtualenv 複製環境 cdsitepackages cd到當前環境的site-packages目錄 lssitepackages 列出當前環境中site-packages內容 setvirtualenvproject 綁定現存的項目和環境 wipeenv 清除環境內全部第三方包