2011-06-19 122 views
18

我已經有不同版本的python使用Macports安裝在我的Mac上。當我通過$ port select python python27選擇python 2.7時,virtualenvwrapper完美運行。使用不同版本的python與virtualenvwrapper

但如果我選擇蟒蛇的另一個版本,即2.6,virtualenvwrapper生成一條錯誤消息:導入錯誤:沒有模塊名爲virtualenvwrapper.hook_loader

我檢查了我的.profile文件,它的設置VIRTUALENVWRAPPER_PYTHON/opt/local/bin/python,所以它似乎我virtualenvwrapper應該工作,不管我選擇了哪個python。

任何想法當我切換python版本會導致virtualenvwrapper生成一個.hook_loader錯誤?

+8

沒有通過'端口選擇...'並堅持你的基地2.7,只是運行'mkvirtualenv --python/path/to/python2.6'工作? 它應該自動切換到(並設置環境)正確的解釋器。在我的系統上(設置自制軟件),'mkvirtualenv -p python2.6'正常工作。 –

+0

我沒有得到hook_loader錯誤,但它抱怨缺少DEST_DIR $ mkvirtualenv --python /opt/local/bin/python2.7 運行virtualenv時帶有解釋器/opt/local/bin/python2.7 您必須提供一個DEST_DIR – wmfox3

+1

哎呀,對不起 - 忽略了關鍵的論點!這應該是'mkvirtualenv --python /path/to/python2.6 env_name'。 mkvirtualenv在你的'$ WORKON_HOME'中創建一個名爲「env_name」的文件夾,並將其作爲'DEST_DIR'參數傳遞給virtualenv。如果沒有指定名稱,那麼很難確定設置的位置,這是肯定的。 –

回答

19

我知道這是你的意見幾乎解決了,但它僅適用於Mac,

,甚至更多,我認爲正確的方法應該是設置VIRTUALENVWRAPPER_PYTHON您所使用的命令行上真正的蟒蛇。

可以肯定你可以做which python

事實上,你甚至可以這樣做:

export VIRTUALENVWRAPPER_PYTHON=`which python` 

在Linux上我在的.bashrc做到這一點,所以這一切的一切,假設你安裝的virtualenv並創建了第一個「虛擬環境」 virtualenv(原如何)

. virtualenv/bin/activate 
export WORKON_HOME=$HOME/.virtualenvs # or whatever else you want 
export VIRTUALENVWRAPPER_PYTHON=`which python` 
export PROJECT_HOME=SOMETHING 
source $HOME/virtualenv/bin/virtualenvwrapper.sh # or wherever else you got that installed 

(順便說一句,你寫道:

I checked my .profile and it's setting VIRTUALENVWRAPPER_PYTHON to /opt/local/bin/python, so it seems to me virtualenvwrapper should work regardless of which python I've selected

WHI ch實際上是相反的 - virtualenv依賴於使用正確的python(以及與之一起使用的包),所以相應地設置python路徑非常重要。

即使運行一個帶有「#!/ bin/python」的py文件,一旦你使用virtualenved,可能會帶來麻煩!

+4

在我的Mac上運行一些測試 - 看起來'VIRTUALENVWRAPPER_PYTHON'只控制'virtualenvwrapper'自己使用哪個Python可執行文件,而不是安裝到虛擬環境中的可執行文件,例如當你運行'mkproject'時。我很想做錯,但到目前爲止,似乎選擇不同的Python版本的唯一方法是在'virtualenvwrapper'命令行中使用'-p/--python'。如果那是真的,那真是一種恥辱。 –

+0

@ChrisJohnson mmmh從那時起我停止使用virtualenvwrapper - 沒有找到太多的需求 - 我沒有簡單的方法來快速複查,但你可能確實是正確的...另外在MAC上我現在使用brew ... – Stefano

+0

same作爲@ChrisJohnson在Ubuntu上。我的'VIRTUALENVWRAPPER_PYTHON'設置爲python2,但'mkvirtualenv'正在用python3創建virtualenvs。 –

-1

你(OP)似乎已經安裝了virtualenv和virtualenvwrapper與python2.7,而不是與python2.6。如果在您的shell加載virtualenvwrapper.sh腳本時調用python2.6,那麼它會很不愉快。非常簡單。

VIRTUALENVWRAPPER_PYTHON是針對這些情況製作的。有了它,你可以確保你總是使用正確的python版本,並且不必總是添加-p /path/to/python2.7

所以,我不同意Stefano在這種情況下的回答,在OP的情況下,你應該已經清楚地在你的.bashrc中使用了哪些python:

... 
export VIRTUALENVWRAPPER_PYTHON=/path/to/your/python2.7 
source /path/to/bin/virtualenvwrapper.sh 

這樣就應該一直都可以! Virtualenvwrapper是爲了簡化事情而完成的。

此外,請注意,/opt/local/bin/python必須是與port python select(請確保與ls -l /opt/local/bin/python確定)選擇的python版本的符號鏈接。

+2

我想強調的是,如果你有分層的終端阻止你設置一個環境變量(如我),使用-p標誌是一個解決方案。 mkvirtualenv -p/usr/bin/python3 Foo – htmldrum

5

確認使用兩個名稱類似的環境變量:

VIRTUALENVWRAPPER_PYTHON - 它的Python版本所使用的virtualenvwrapper實用程序本身。

VIRTUALENV_PYTHON - 當您創建新的虛擬環境時,將通過virtualenv安裝哪個Python版本。相當於virtualenv命令行上的-p/--python選項。

也許很明顯:)在虛擬環境中運行的Python版本是您爲其安裝的版本 - 在env創建後與上述環境變量無關。

請參閱https://stackoverflow.com/a/24724360/763269瞭解如何在virtualenv中升級Python。

6

這些都沒有工作。我在安裝我的OSX機器時首先安裝了Python3,並且所有的默認都是pip。

首先,檢查哪些Python中,你已經安裝:

$ `which python` -V 

如果返回 「巨蟒2.7.12」,然後就被設置爲運行:

$ mkvirtualenv -p `which python` api_server 
Running virtualenv with interpreter /usr/local/bin/python 
New python executable in /Users/eric/.virtualenvs/api_server/bin/python2.7 
Also creating executable in /Users/eric/.virtualenvs/api_server/bin/python 
Installing setuptools, pip, wheel...done. 
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/predeactivate 
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/postdeactivate 
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/preactivate 
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/postactivate 
virtualenvwrapper.user_scripts creating /Users/eric/.virtualenvs/api_server/bin/get_env_details 

這也將激活api_server workon,它改變你的python可執行文件:

$ which python 
/Users/eric/.virtualenvs/api_server/bin/python 
$ python -V 
Python 2.7.12 

是什麼其實呢?它輸出在PATH中發現的蟒蛇可執行文件的目錄:

$ which python 
/usr/local/bin/python 

使用which python,你基本上傳遞/usr/local/bin/python在mkvirtualenv目錄-p選項。

當您在which python中返回多個python可執行文件時會發生什麼?只要找到一個你想要的,並傳遞它:

$ mkvirtualenv -p /usr/local/bin/python3 api_server 

而且virtualenvwrapper最終會使用Python的可執行文件來代替。

相關問題