簡體   English   中英

Python Virtualenv - 沒有名為virtualenvwrapper.hook_loader的模塊

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

我正在運行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

任何幫助,將不勝感激。

該問題已按照以下步驟解決:

#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

另外,如果你有使用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 然后在你的.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

這發生在我身上,我通過重新安裝pip解決了這個問題。 發生了什么是which pip給了/usr/bin/pip ,而which python給了/usr/local/bin/python pip的路徑應該是/usr/local/bin/pip 當我更新我的Python安裝時,這可能會破壞。

如果您按照pip文檔進行操作 ,則可以輕松地為當前正在運行的Python設置重新安裝pip 你需要:

  1. 下載get-pip.py腳本(直接從pip的文檔鏈接)。
  2. 運行python get-pip.py

這解決了我的問題。

有許多事情可能導致此錯誤。 如果你的環境是

  • CentOS 7,從epel-release安裝了python3
  • pip3安裝python3.4 get-pip.py
  • virtualenvwrapperpip3安裝
  • 使用mkvirtualenv -p /usr/bin/python3.4 python虛擬環境

然后,無論出於何種原因,在沒有virtualenvwrapper庫的情況下創建虛擬環境。 您可以通過再次安裝來解決它,但這次是在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

對於任何使用Ubuntu 18.04Python 3+的人來說 ,這對我有用

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

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

對我來說這很簡單:

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

我犯了同樣的錯誤 。 發現我有舊版的點子。 我只是通過升級pip來修復錯誤。

我剛剛安裝了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路徑中的'local'會如何影響我從長遠來看,但是現在,它有效。

卸載 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則不會重新創建。 相反,您需要做的是在卸載virtualenvwrapper包時purge它。 就像Debian上的這樣:

apt-get remove --purge virtualenvwrapper

嘗試卸載你的virtualenvvirtualenvwrapper並使用2.7版本中的pip再次安裝它(我認為)。

我遇到了同樣的錯誤,我剛剛做了這個並解決了我的問題。

我用U.

即使有一個公認的答案,我認為我會為我修復它。

首先我安裝了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的另一部分中,並且啟動時錯誤仍然存​​在。

安裝Conda / Anaconda項目后遇到類似的問題。 這個問題是在解決MAC.Resolving問題有我我的問題非常有幫助的.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,如其他答案中所強調的那樣。

剛剛在Centos 7.4上碰到了這個問題。

上述答案都不適合我的情況。 經過相當多的挖掘后,我在python庫中確定了過於嚴格的文件權限(我猜Centos上的python安裝與其他POSIX系統有點不同)。

因此,如果其他一切都失敗了,您可能需要檢查您嘗試運行virtualenvwrapper的用戶是否可讀取您的python庫。

特別檢查: /usr/lib/python3.6 /usr/lib64/python3.6 (修改不同python版本的路徑)。

如果您看到該groupothers缺少讀取和執行權限,則添加它們: sudo chmod og+rx -R /usr/lib/python3.6 sudo chmod og+rx -R /usr/lib64/python3.6

注意:我不確定這是否適用於Centos安全策略,但只要您不提供write持久性,它就可能是安全的。

在我的情況下(OS X 10.13.6),這樣做了

brew install python2 --upgrade

我遇到了與此問題相同的問題,並花了很多時間來配置錯誤。 我終於發現了什么是錯的。

首先,我找到了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 PATH的含義

正如您在上面的鏈接中看到的,PYTHONPATH擴充了模塊的默認搜索路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM