80

我正在运行 Mac OS 10.6.8。除了 python 2.6 之外,还想安装 python 2.7 并在新的 virtualenv 中使用 python 2.7。我执行了以下步骤:

我下载了python 2.7并安装了它:

http://www.python.org/ftp/python/2.7.3/python-2.7.3-macosx10.6.dmg

然后我运行命令来使用 python2.7 设置一个新的 virtualenv:

mkvirtualenv --python=python2.7 mynewenv

我的 .bash_profile 如下所示:

# needed for virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh


# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

现在,当我打开控制台时,我收到以下错误消息。

ImportError: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is set properly.

我还在另一篇文章中发现我应该升级 virtualenvwrapper。那没有帮助。

sudo pip install virtualenvwrapper --upgrade

任何帮助,将不胜感激。

4

16 回答 16

53

该问题已按照以下步骤解决:

#switch the /usr/bin/python link to point to current python link
cd /usr/bin
sudo mv python python.bak
sudo ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/python python

重新排列导出命令,使其位于我的 .bash_profile 文件中的 virtualenv 命令之前:

PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH
export PATH

# needed for virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

重新安装 setuptools,轻松安装和 PIP。为了使它们与新的 python 版本正常工作,这显然是必需的:

sudo sh setuptools-0.6c11-py2.7.egg

sudo easy_install-2.7 pip

pip install virtualenv
于 2012-07-16T21:06:47.767 回答
23

此外,如果您有 macports,请确保在 PATH之前和中/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin列出。然后在你中设置以下内容:/Library/Frameworks/Python.framework/Versions/2.7/bin/usr/local/bin.profile

export VIRTUALENVWRAPPER_PYTHON=`which python`
export VIRTUALENVWRAPPER_VIRTUALENV=`which virtualenv`
source `which virtualenvwrapper.sh`
于 2013-09-05T05:00:25.877 回答
14

对于任何使用Ubuntu 18.04Python 3+的人来说,这对我有用:

which python3 # outputs /usr/bin/python3 
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3  
source `which virtualenvwrapper.sh`  
于 2019-01-07T01:10:53.600 回答
9

就我而言,将此行添加到我的 .zshrc 文件中就可以了,

export VIRTUALENVWRAPPER_PYTHON=/usr/local/Cellar/python/2.7.13/bin/python2.7
于 2017-04-21T18:03:48.693 回答
4

这发生在我身上,我通过重新安装解决了它pip。结果是which pip/usr/bin/pip了,而which python给了/usr/local/bin/python。的路径pip应该是/usr/local/bin/pip。当我更新我的 Python 安装时,这可能会坏掉。

如果您遵循 pip 文档,您可以轻松地pip为当前工作的 Python 设置重新安装。你需要:

  1. 下载get-pip.py脚本(直接从pip的文档链接)。
  2. 运行python get-pip.py

这为我解决了这个问题。

于 2015-04-21T03:58:32.473 回答
3

There are a number of things that can cause this error. If your environment is

  • CentOS 7, with python3 installed from epel-release
  • pip3 installed with python3.4 get-pip.py
  • virtualenvwrapper installed with pip3
  • A python virtual environment made with mkvirtualenv -p /usr/bin/python3.4

Then, for whatever reason, the virtual environment is created without the virtualenvwrapper library. You can solve it by simply installing it again, but this time from within the virtlualenv

[user@localhost ~] $ mkvirtualenv -p /usr/bin/python3.4 venv
Using base prefix '/usr'
New python executable in /home/user/.virtualenvs/venv/bin/python3.4
Also creating executable in /home/user/.virtualenvs/venv/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/preactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/postactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/venv/bin/get_env_details
/home/user/.virtualenvs/venv/bin/python3.4: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper')
/home/user/.virtualenvs/venv/bin/python3.4: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper')

# the virtualenv should now activated
(venv)[user@localhost ~] $ pip install virtualenvwrapper
于 2016-01-27T16:30:46.410 回答
2

我只需要确保 /usr/local/bin/python 存在。

对我来说,这很简单:

ln -s /usr/local/bin/python2.7 /usr/local/bin/python
于 2017-08-13T22:14:54.547 回答
1

我犯了同样的错误 。发现我有旧版本的 pip 。我通过简单地升级 pip 来修复错误。

于 2016-12-27T08:07:26.250 回答
0

刚刚在 Centos 7.4 上遇到了这个问题。

以上答案都不适合我的情况。经过相当多的挖掘后,我发现这是因为 python 库中的文件权限过于严格(我猜 Centos 上的 python 安装与其他 POSIX 系统有点不同)。

因此,如果其他一切都失败了,您可能需要检查您的 python 库是否可以被您尝试运行 virtualenvwrapper 的用户读取。

特别检查:( /usr/lib/python3.6 /usr/lib64/python3.6 修改不同python版本的路径)。

如果您看到它group并且others在那里缺少读取和执行权限,请添加它们: sudo chmod og+rx -R /usr/lib/python3.6 sudo chmod og+rx -R /usr/lib64/python3.6

注意:我不确定这是否违反 Centos 安全策略,但只要您不提供权限,它可能是安全的write

于 2018-05-28T09:32:13.593 回答
0

在我的情况下(OS X 10.13.6),这样做了

brew install python2 --upgrade
于 2018-09-15T18:15:25.757 回答
0

我遇到了与这个问题相同的问题,并且花了很多时间来配置问题所在。我终于发现出了什么问题。

首先,我寻找 virtualenvwrapper 文件夹存在的位置。在我的情况下 /usr/local/lib/python3.7/site-packages。文件夹内是导致错误的 hook_loader.py。

接下来,我使用了python脚本。

python3

import sys;print('\n'.join(sys.path))

我找不到 /usr/local/lib/python3.7/site-packages 目录所以,最后我写了,

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages

到 .bashrc 文件。完毕。

Python 路径的含义

正如您在上面的链接中看到的,PYTHONPATH 增加了模块的默认搜索路径。

于 2019-05-30T09:06:28.823 回答
0

Try to uninstall your virtualenv and virtualenvwrapper and install it again using pip in version 2.7 (I think).

I encountered the same error and I just did this and solved my problem.

I using U

于 2017-01-18T05:33:28.710 回答
0

卸载virtualenvwrapper软件包后我遇到了这个问题。当我登录到任何用户(或其他用户su)时,我会得到:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named virtualenvwrapper.hook_loader                                                                                                                                                                       
virtualenvwrapper.sh: There was a problem running the initialization hooks.                                                                                                                                                      

If Python could not import the module virtualenvwrapper.hook_loader,                                                                                                                                                             
check that virtualenv has been installed for                                                                                                                                                                                     
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is                                                                                                                                                                        
set properly.

解决方案是删除该/etc/bash_completion.d/virtualenvwrapper文件。

编辑:

请勿删除上述文件,否则重新安装不会重新创建virtualenvwrapper。相反,您需要做的是卸载它时purge的包。virtualenvwrapper像这样在 Debian 上:

apt-get remove --purge virtualenvwrapper
于 2016-07-06T19:43:58.480 回答
0

我刚刚安装了 python 3.5,尝试了 virtualenvwrapper,然后遇到了这个问题。我开始意识到 python3.5 安装在/usr/local/bin/python3.5而不是/usr/bin/python3.5. 所以,我修改了我的 .bash_profile 脚本,如下所示,现在一切似乎都正常了

# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.5
export WORKON_HOME=$HOME/.virtualenvs
source /Users/bentaub/.virtualenvs/djangodev/bin/virtualenvwrapper.sh

我已经够新手了,不确定从长远来看,python3.5 路径中的“本地”会如何影响我,但就目前而言,它是有效的。

于 2016-01-23T20:54:10.967 回答
0

安装 Conda/Anaconda 项目后遇到类似问题。这个问题对解决我在 MAC 上的问题很有帮助。解决这个问题后,我的.zshrc相关部分如下所示:

export VIRTUALENVWRAPPER_PYTHON=$HOME/Applications/conda/bin/python
source $HOME/Applications/conda/bin/virtualenvwrapper.sh

这取决于我安装 conda 的位置,您必须根据自己的情况来计算。要根据安装 anaconda 的位置获取给定环境的详细信息,可以使用以下命令:

$  ~/ -name virtualenvwrapper.sh # to see where you have this. May already be prefilled in your shell profile[.zshrc or .profile]

$ which python   # to know the default python your project or rather where conda has installed python for you

不要忘记卸载和安装 virtualenv 和 virtualenvwrapper,如其他答案中突出显示的那样。

于 2018-01-22T15:45:22.367 回答
0

即使有一个公认的答案,我想我会为我解决这个问题。

首先,我安装了 Python 并刚刚通过 Homebrew对其进行了升级。我也在使用 ZSH,所以如果某些位与您的输出不完全匹配,那么这可能就是原因。

通过运行brew info python并查看输出,我发现了以下不错的信息:

If you wish to have this formula's python executable in your PATH then add
the following to ~/.zshrc:
    export PATH="/usr/local/opt/python/libexec/bin:$PATH"

所以我将它添加到我的终端启动脚本中,如图所示,错误 n 不再显示。

注意:我将它插入到我的 PATH 的另一部分,并且错误在启动时仍然存在。

于 2017-08-23T14:55:47.423 回答