virtualenvwrapper配置報錯
在安裝好virtualenvwrapper之後,需要在~/.bashrc中配置,把vw的.sh文件添加進去,還可以選擇修改虛擬環境默認保存的位置。
首先在編輯.bashrc之前,你需要知道virtualenvwrapper.sh文件的路徑,使用下面的命令查找。
sudo find / -name virtualenvwrapper.sh
下面是我的查找結果
因爲我是直接安裝在我的用戶路徑下的,所以如果你是安裝在根目錄下,結果應該是/usr/local/bin/virtualenvwrapper.sh。
得知路徑之後就可以在~/.bashrc文件中添加下面配置
export WORKON_HOME=$HOME/virtualenvs # 配置虛擬環境的保存位置 $HOME是用戶的主目錄
source /home/God/.local/bin/virtualenvwrapper.sh # 配置virtualenvwrapper命令的腳本
第一條配置可以隨你的喜愛去更改,但是第二條配置需要使用剛纔獲取路徑,只有命令腳本路徑正確纔可在linux中直接使用virtualenvwrapper中的命令。
修改.bashrc文件後,需要重新加載.bashrc文件
source ~/.bashrc
重點!!!
如果你的環境是python3,重新加載後,可能會出現這樣類似的錯誤
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
因爲在virtualenvwrapper.sh中有如下代碼
# Locate the global Python where virtualenvwrapper is installed.
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
腳本會默認使用python2環境,但是virtualenvwrapper裝在了python3環境中,所以會有上面的報錯。
解決辦法:
直接將VIRTUALENVWRAPPER_PYTHON默認值修改爲/usr/bin/python3即可
# Locate the global Python where virtualenvwrapper is installed.
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
修改後,按照上面步驟,重新加載下virtualenvwrapper.sh即可解決問題。