(坑集)virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not

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即可解决问题。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章