在OS X Lion上使用virtualenvwrapper时出现的错误信息

15

我使用Homebrew在新的Mac Lion安装中安装了Python,并尝试使用pip安装virtualenv和virtualenvwrapper,但是当我启动新的终端会话时,我收到了这个回溯:

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.

我使用的Python和pip来自Homebrew,但是似乎它想让我使用苹果默认的Python。我遇到了以下问题

~$ which python | xargs ls -l
lrwxr-xr-x  1 beard  admin  33 Jun 24 16:11 /usr/local/bin/python -> ../Cellar/python/2.7.3/bin/python
~$ echo $VIRTUALENVWRAPPER_PYTHON
/usr/local/bin/python
~$ which pip | xargs ls -l
-rwxr-xr-x  1 beard  admin  301 Jun 24 16:18 /usr/local/share/python/pip
~$ which virtualenvwrapper.sh | xargs ls -l
-rwxr-xr-x  1 beard  admin  32227 Jun 24 16:19 /usr/local/share/python/virtualenvwrapper.sh

然而,它似乎认为我已经使用系统Python在/usr/bin/python中安装了pip(和virtualenv)。

编辑:在我的bashrc文件中--

export WORKON_HOME="$HOME/py-env"
export VIRTUALENVWRAPPER_LOG_DIR="$HOME/py-env"
export VIRTUALENVWRAPPER_HOOK_DIR="$HOME/py-env"
source /usr/local/share/python/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_PYTHON="/usr/local/bin/python"
5个回答

20

因为您有自己的Python版本,所以您是否尝试过覆盖VIRTUALENVWRAPPER_PYTHON?(看起来您想要export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/pythonvirtualenvwrapper文档建议在执行source virtualenvwrapper.sh之前将该环境变量设置为所需的Python。他们的示例如下:

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh

我在我的bashrc中设置了virtualenvwrapper_python变量(刚刚编辑的问题)。 - beardc
1
这可能有点傻,但看起来您在源命令之后有导出命令,因此在调用virtualenvwrapper.sh时将无法看到正确的python值。尝试将源代码移动到最后? - munchybunch

1
将路径添加或更改为/usr/local/share/python/virtualenvwrapper.sh,以便在~/.bash_profile中使用virtualenvwrapper.sh。
应该如下所示:
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export WORKON_HOME=$HOME/.virtualenvs

source /usr/local/share/python/virtualenvwrapper.sh

0

我在macOS Catalina上遇到了类似的问题。它自带zsh和Python 2.7。

通过以下步骤,我安装了Python3和虚拟环境:

安装Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

安装Python

brew install python

检查Python的安装位置

brew show python

压缩的输出

==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python

macOS 上 Python 的默认路径是 /usr/bin/python,它指向 Python 2.7。因此,我们需要创建一个从 python 到 python3 的别名。

alias python=/usr/local/bin/python3

然后按照@munchybunch的上述步骤进行操作,

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

通过上述步骤,我成功创建了一个虚拟环境。
mkvirtualenv dev

0
如果您正在使用PythonBrew&VirtualEnvBurrito与ZSH shell,请确保检查您的.zprofile文件 - 这包含一些命令以执行VirtualEnvBurrito启动脚本。由于.zprofile在.zshrc之前加载,因此Python版本是系统版本。这也会引发此错误。

0
我在使用 OS X Lion 操作系统时遇到了 Homebrew 安装的 Python 与系统安装的 Python 冲突的类似问题。我从未能够通过 Homebrew 解决这个问题。然而,当我转而使用 pythonbrew(https://github.com/utahta/pythonbrew)来管理我安装的 Python(而不是使用 Homebrew),我成功地运行了 virtualenvwrapper。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接