Python建立虛擬環境

virtualenv安裝及使用[不建議使用]

安裝 virtualenv

pip install virtualenv

建立虛擬環境venv

virtualenv venv

複製選擇的Python爲虛擬環境

virtualenv -p /usr/bin/python2.7 venv

在默認狀況下,全部安裝在系統範圍內的包對於virtualenv是可見的。 這意味着若是你將simplejson安裝在您的系統Python目錄中,它會自動提供給全部的virtualenvs使用。 這種行爲能夠被更改,在建立virtualenv時增長 --no-site-packages 選項的virtualenv就不會讀取系統包python

建立虛擬環境(虛擬環境再也不讀取系統Python的包)

virtualenv venv --no-site-packages

激活或者使用虛擬環境

source venv/bin/activate

退出虛擬環境

deactivate

virtualenvwrapper

因爲virtualenv用起來有點麻煩,wrapper對它進行了封裝,讓它更好用,最終咱們使用wrapper提供的命令,可是實際工做都是virtualenv作的react

安裝須要依賴

使用管理員權限執行linux

# pip install virtualenv virtualenvwrapper

退出管理員,在~/.bashrc文件的末尾添加一下配置

配置環境

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   #刪除虛擬空間

下面是virtualenvwrapper的基本命令

workon 切換到環境
deactivate 註銷當前環境
lsvirtualenv 列出全部環境
rmvirtualenv 刪除環境
cpvirtualenv 複製環境
cdsitepackages cd到當前環境的site-packages目錄
lssitepackages 列出當前環境中site-packages內容
setvirtualenvproject 綁定現存的項目和環境
wipeenv 清除環境內全部第三方包
相關文章
相關標籤/搜索