简体   繁体   English

Python Virtualenv - 没有名为virtualenvwrapper.hook_loader的模块

[英]Python Virtualenv - No module named virtualenvwrapper.hook_loader

I'm running Mac OS 10.6.8. 我正在运行Mac OS 10.6.8。 and wanted to install in addition to python 2.6 also python 2.7 and use python 2.7 in a new virtualenv. 并希望除了python 2.6还安装python 2.7并在新的virtualenv中使用python 2.7。 I executed the following steps: 我执行了以下步骤:

I downloaded python 2.7 and installed it: 我下载了python 2.7并安装了它:

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

Then I run the command to setup a new virtualenv using python2.7: 然后我运行命令使用python2.7设置一个新的virtualenv:

mkvirtualenv --python=python2.7 mynewenv

My .bash_profile looks like the following: 我的.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

Now when I open the console I get the following error message. 现在,当我打开控制台时,我收到以下错误消息。

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.

I also found in a different post that I should upgrade virtualenvwrapper. 我还在另一篇文章中发现我应该升级virtualenvwrapper。 That did not help. 这没有用。

sudo pip install virtualenvwrapper --upgrade

Any help would be appreciated. 任何帮助,将不胜感激。

The issue was solved following the steps below: 该问题已按照以下步骤解决:

#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

Re-arrange the export command in order that it is placed before the virtualenv commands in my .bash_profile file: 重新排列导出命令,以便将它放在我的.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

Re-Install setuptools, easy install and PIP. 重新安装setuptools,易于安装和PIP。 This is obviously needed in order that they work properly with the new python version: 显然需要这样才能使它们与新的python版本一起正常工作:

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

sudo easy_install-2.7 pip

pip install virtualenv

Also, if you have macports, make sure /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin is listed before /Library/Frameworks/Python.framework/Versions/2.7/bin and /usr/local/bin in PATH. 另外,如果你有使用MacPorts,确保/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin在上市前/Library/Frameworks/Python.framework/Versions/2.7/bin/usr/local/bin在PATH中的/usr/local/bin Then set the following in you .profile : 然后在你的.profile设置以下内容:

export VIRTUALENVWRAPPER_PYTHON=`which python`
export VIRTUALENVWRAPPER_VIRTUALENV=`which virtualenv`
source `which virtualenvwrapper.sh`

在我的情况下,将此行添加到我的.zshrc文件中就可以了,

export VIRTUALENVWRAPPER_PYTHON=/usr/local/Cellar/python/2.7.13/bin/python2.7

This happened to me and I solved it by re-installing pip . 这发生在我身上,我通过重新安装pip解决了这个问题。 What had happend was that which pip gave /usr/bin/pip as a result, while which python gave /usr/local/bin/python . 发生了什么是which pip给了/usr/bin/pip ,而which python给了/usr/local/bin/python The path for pip should be /usr/local/bin/pip . pip的路径应该是/usr/local/bin/pip This probably broke when I updated my Python installation. 当我更新我的Python安装时,这可能会破坏。

If you follow the pip documentation you can easily reinstall pip for your current working Python setup. 如果您按照pip文档进行操作 ,则可以轻松地为当前正在运行的Python设置重新安装pip You need to: 你需要:

  1. Download the get-pip.py script (directly linked from pip 's documentation). 下载get-pip.py脚本(直接从pip的文档链接)。
  2. Run python get-pip.py . 运行python get-pip.py

This solved the problem for me. 这解决了我的问题。

There are a number of things that can cause this error. 有许多事情可能导致此错误。 If your environment is 如果你的环境是

  • CentOS 7, with python3 installed from epel-release CentOS 7,从epel-release安装了python3
  • pip3 installed with python3.4 get-pip.py pip3安装python3.4 get-pip.py
  • virtualenvwrapper installed with pip3 virtualenvwrapperpip3安装
  • A python virtual environment made with mkvirtualenv -p /usr/bin/python3.4 使用mkvirtualenv -p /usr/bin/python3.4 python虚拟环境

Then, for whatever reason, the virtual environment is created without the virtualenvwrapper library. 然后,无论出于何种原因,在没有virtualenvwrapper库的情况下创建虚拟环境。 You can solve it by simply installing it again, but this time from within the virtlualenv 您可以通过再次安装来解决它,但这次是在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

For anyone using Ubuntu 18.04 and Python 3+ , this did the trick for me: 对于任何使用Ubuntu 18.04Python 3+的人来说 ,这对我有用

which python3 # outputs /usr/bin/python3 
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3  
source `which virtualenvwrapper.sh`  

I just had to make sure that /usr/local/bin/python existed. 我只需要确保/ usr / local / bin / python存在。

For me it was a simple: 对我来说这很简单:

ln -s /usr/local/bin/python2.7 /usr/local/bin/python

I get the same error . 我犯了同样的错误 。 Found out I had old version of pip . 发现我有旧版的点子。 I fixed the error by simply upgrading the pip . 我只是通过升级pip来修复错误。

I just installed python 3.5,tried the virtualenvwrapper and then had this problem. 我刚刚安装了python 3.5,尝试了virtualenvwrapper然后遇到了这个问题。 I came to realize that python3.5 was installed in /usr/local/bin/python3.5 and NOT /usr/bin/python3.5 . 我开始意识到python3.5安装在/usr/local/bin/python3.5而不是/usr/bin/python3.5 So, I revised my .bash_profile script to look like the following and everything seems to work now 所以,我将我的.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

I'm enough of a novice to not be sure how that 'local' in the path to python3.5 is going to affect me in the long run but, for now, it works. 我已经足够新手不能确定python3.5路径中的'local'会如何影响我从长远来看,但是现在,它有效。

I had this problem after uninstalling the virtualenvwrapper package. 卸载 virtualenvwrapper包后我遇到了这个问题。 When I logged in to any user (or su to a different one), I would get: 当我登录任何用户(或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.

The solution was to delete the /etc/bash_completion.d/virtualenvwrapper file. 解决方案是删除 /etc/bash_completion.d/virtualenvwrapper文件。

Edit: 编辑:

Do not delete the above file or it won't be recreated if you reinstall virtualenvwrapper . 不要删除上述文件,否则如果重新安装virtualenvwrapper则不会重新创建。 Instead what you need to do is purge the virtualenvwrapper package when you uninstall it. 相反,您需要做的是在卸载virtualenvwrapper包时purge它。 Like this on Debian: 就像Debian上的这样:

apt-get remove --purge virtualenvwrapper

Try to uninstall your virtualenv and virtualenvwrapper and install it again using pip in version 2.7 (I think). 尝试卸载你的virtualenvvirtualenvwrapper并使用2.7版本中的pip再次安装它(我认为)。

I encountered the same error and I just did this and solved my problem. 我遇到了同样的错误,我刚刚做了这个并解决了我的问题。

I using U 我用U.

Even though there is an accepted answer I thought I would put what fixed it for me. 即使有一个公认的答案,我认为我会为我修复它。

Firstly I installed Python and had just upgraded it via Homebrew . 首先我安装了Python,并通过Homebrew升级了它。 I am also using ZSH so if some bits don't quite match your output then that might be why. 我也在使用ZSH,所以如果某些位与输出不匹配那么这可能就是原因。

By running brew info python and looking through the output I found the following nice bit of information: 通过运行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"

So I added this to my terminal startup script as shown and the error n longer displays. 所以我将其添加到我的终端启动脚本中,如图所示,错误n显示更长。

Note: I inserted this into another part of my PATH and the error persisted on start up. 注意:我将其插入到PATH的另一部分中,并且启动时错误仍然存​​在。

Ran into a similar issue after installing Conda/Anaconda project. 安装Conda / Anaconda项目后遇到类似的问题。 This question was quite helpful in resolving my issue on MAC.Resolving the issue had my .zshrc relevant portion looking like this: 这个问题是在解决MAC.Resolving问题有我我的问题非常有帮助的.zshrc相关的部分看起来像这样:

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

This is depended on where I have conda installed and you'll have to figure that in your own case. 这取决于我安装conda的位置,你必须在你自己的情况下计算。 To get the specifics for your given environment depending on where you've installed anaconda you can use the following: 要根据您安装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

DON'T FORGET TO UNINSTALL AND INSTALL virtualenv and virtualenvwrapper as highlighted in other answers. 不要忘记卸载和安装virtualenv和virtualenvwrapper,如其他答案中所强调的那样。

Just bumped into this issue on a Centos 7.4. 刚刚在Centos 7.4上碰到了这个问题。

None of the above answers suited my case. 上述答案都不适合我的情况。 After quite a bit of digging around I pinpointed this to too-strict file permissions in python libs (I guess python installation on Centos differs a bit from other POSIX systems). 经过相当多的挖掘后,我在python库中确定了过于严格的文件权限(我猜Centos上的python安装与其他POSIX系统有点不同)。

So, if everything else fails you might want to check that your python libs are readable by the user you're trying to run virtualenvwrapper with. 因此,如果其他一切都失败了,您可能需要检查您尝试运行virtualenvwrapper的用户是否可读取您的python库。

In particular check: /usr/lib/python3.6 /usr/lib64/python3.6 (amend the paths for different python versions). 特别检查: /usr/lib/python3.6 /usr/lib64/python3.6 (修改不同python版本的路径)。

If you see that group and others lack read and execute permissions in there then add them: sudo chmod og+rx -R /usr/lib/python3.6 sudo chmod og+rx -R /usr/lib64/python3.6 如果您看到该groupothers缺少读取和执行权限,则添加它们: sudo chmod og+rx -R /usr/lib/python3.6 sudo chmod og+rx -R /usr/lib64/python3.6

Note: I'm not sure whether this works against a Centos security policy but it's probably safe as long as you don't give write persmisions. 注意:我不确定这是否适用于Centos安全策略,但只要您不提供write持久性,它就可能是安全的。

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

brew install python2 --upgrade

I had the same problem as this one and spent so much time on configuring out what was wrong. 我遇到了与此问题相同的问题,并花了很多时间来配置错误。 And I finally found out what was wrong. 我终于发现了什么是错的。

First I looked for where virtualenvwrapper folder exists. 首先,我找到了virtualenvwrapper文件夹存在的位置。 In my case /usr/local/lib/python3.7/site-packages. 在我的情况下/usr/local/lib/python3.7/site-packages。 Inside the folder is hook_loader.py that caused the error. 文件夹内部是hook_loader.py导致错误。

Next, I used python script. 接下来,我使用了python脚本。

python3

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

I couldn't find the /usr/local/lib/python3.7/site-packages directory so, at last I wrote, 我找不到/usr/local/lib/python3.7/site-packages目录,最后我写道,

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

to .bashrc file. 到.bashrc文件。 Done. 完成。

Meaning of PYTHON PATH PYTHON PATH的含义

As you can see in above link, PYTHONPATH augment the default search path for modules. 正如您在上面的链接中看到的,PYTHONPATH扩充了模块的默认搜索路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 无法导入模块 virtualenvwrapper.hook_loader? - Python could not import the module virtualenvwrapper.hook_loader? /usr/bin/python3:查找“virtualenvwrapper.hook_loader”规范时出错(<class 'importerror'> : 没有名为“virtualenvwrapper”的模块)</class> - /usr/bin/python3: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper') 无法导入模块virtualenvwrapper.hook_loader - Cannot import module virtualenvwrapper.hook_loader 当我将 python3.7 降级到 python3.6 时,查找“virtualenvwrapper.hook_loader”的模块规范时出错 - Error while finding module specification for 'virtualenvwrapper.hook_loader' when I downgrade python3.7 to python3.6 安装了 virtualenv 和 virtualenvwrapper:Python 说没有名为 virtualenvwrapper 的模块 - Installed virtualenv and virtualenvwrapper: Python says no module named virtualenvwrapper Virtualenvwrapper:没有名为virtualenvwrapper的模块 - Virtualenvwrapper: no module named virtualenvwrapper 更新后 python 和 virtualenvwrapper 的问题:没有名为 virtualenvwrapper 的模块 - problems with python and virtualenvwrapper after updating: No module named virtualenvwrapper ImportError:没有名为“ virtualenvwrapper”的模块 - ImportError: No module named 'virtualenvwrapper' 没有名为“virtualenvwrapper”的模块 - No module named 'virtualenvwrapper' Python virtualenv没有名为django的模块 - Python virtualenv no module named django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM