由于在ubuntu环境下,将python做与python3.6做了软链接(ln -s python python3.6),并且pip也被我做了软链接,所以导致用pip安装virtualenvwrapper之后,在source启动virtualenvwrapper.sh时以及workon 虚拟环境时总是报错:
./virtualenvwrapper.sh: line 230: : command not found
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= and that PATH is
set properly.
这是根据提示230行的语句:
"$VIRTUALENVWRAPPER_PYTHON" -m 'virtualenvwrapper.hook_loader' \
结合错误信息与提示找到的语句,猜测应该是VIRTUALENVWRAPPER_PYTHON这里有问题,然后在virtualenvwrapper.sh文件中查找VIRTUALENVWRAPPER_PYTHON,发现了关键点:
# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
VIRTUALENVWRAPPER_PYTHON="$(command \which python3)" # 原本是写的\which python,这里贴出来的是我修改为python3后的。
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.