由於在ubuntu環境下,將python做與python3.6做了軟鏈接(ln -s python python3.6),並且pip也被我做了軟鏈接,所以導致用pip安裝virtualenvwrapper之後,在source啟動virtualenvwrapper.sh時以及workon 虛擬環境時總 ...
由於在ubuntu環境下,將python做與python3.6做了軟鏈接(ln -s python python3.6),並且pip也被我做了軟鏈接,所以導致用pip安裝virtualenvwrapper之後,在source啟動virtualenvwrapper.sh時以及workon 虛擬環境時總是報錯:(這裡建議直接用pip3 安裝virtualenv和virtualenvwrapper)
1 ./virtualenvwrapper.sh: line 230: : command not found 2 virtualenvwrapper.sh: There was a problem running the initialization hooks. 3 4 If Python could not import the module virtualenvwrapper.hook_loader, 5 check that virtualenvwrapper has been installed for 6 VIRTUALENVWRAPPER_PYTHON= and that PATH is 7 set properly.
這是根據提示230行的語句:
1 "$VIRTUALENVWRAPPER_PYTHON" -m 'virtualenvwrapper.hook_loader' \
結合錯誤信息與提示找到的語句,猜測應該是VIRTUALENVWRAPPER_PYTHON這裡有問題,然後在virtualenvwrapper.sh文件中查找VIRTUALENVWRAPPER_PYTHON,發現了關鍵點:
1 # Locate the global Python where virtualenvwrapper is installed.
2 if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
3 then
4 VIRTUALENVWRAPPER_PYTHON="$(command \which python3)" # 原本是寫的\which python,這裡貼出來的是我修改為python3後的。
5 fi
VIRTUALENVWRAPPER_PYTHON是用來(Locate the global Python where virtualenvwrapper is installed.)定位哪個python下麵安裝了virtualenvwrapper的。原本指定的位置是python,也就是2.7版本的。鑒於之前我使用python3.6安裝的,所以此處要改成python3。然後這個錯誤就消失了。
使用virtualenvwrapper的好處是不用每次使用source /xxx/虛擬環境/bin/activate來啟動虛擬環境了,在~/.bashrc配置一下,日後開啟虛擬環境直接可以用workon命令即可,具體操作步驟,前提是你已經安裝了python-virtualenv了:
# Setup: # 1. Create a directory to hold the virtual environments. # (mkdir $HOME/.virtualenvs). # 2. Add a line like "export WORKON_HOME=$HOME/.virtualenvs" # to your .bashrc. # 3. Add a line like "source /path/to/this/file/virtualenvwrapper.sh" # to your .bashrc. # 4. Run: source ~/.bashrc # 5. Run: workon # 6. A list of environments, empty, is printed. # 7. Run: mkvirtualenv temp # 8. Run: workon # 9. This time, the "temp" environment is included. # 10. Run: workon temp # 11. The virtual environment is activated.