2014-02-07 62 views
10

我創建了一個虛擬的環境與virtualenvwrapper,然後我嘗試在它與pip安裝Django。不過,由於python版本的衝突,我一直收到一個錯誤。PIP執行虛擬ENV內部錯誤Python庫版本

$ mkvirtualenv env 
$ workon env 
$ pip install django 
Downloading/unpacking django 
Cleaning up... 
Exception: 
Traceback (most recent call last): 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/basecommand.py", line 134, in main 
    status = self.run(options, args) 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/commands/install.py", line 236, in run 
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle) 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/req.py", line 1085, in prepare_files 
    url = finder.find_requirement(req_to_install, upgrade=self.upgrade) 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/index.py", line 201, in find_requirement 
    page = self._get_page(main_index_url, req) 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/index.py", line 554, in _get_page 
    return HTMLPage.get_page(link, req, cache=self.cache) 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/index.py", line 671, in get_page 
    resp = urlopen(url) 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/download.py", line 176, in __call__ 
    response = self.get_opener(scheme=scheme).open(url) 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/download.py", line 238, in get_opener 
    headers.append(("User-agent", build_user_agent())) 
    File "/Users/mingot/virtualenvs/env/lib/python2.7/site-packages/pip/download.py", line 35, in build_user_agent 
    _implementation = platform.python_implementation() 
    File "/Users/mingot/soft/anaconda/lib/python2.7/platform.py", line 1486, in python_implementation 
    return _sys_version()[0] 
    File "/Users/mingot/soft/anaconda/lib/python2.7/platform.py", line 1451, in _sys_version 
    repr(sys_version)) 
ValueError: failed to parse CPython sys.version: '2.7.5 (default, Aug 25 2013, 00:04:04) \n[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]' 

在該系統中,我正在蟒水蟒:

$ python 
Python 2.7.5 |Anaconda 1.8.0 (x86_64)| (default, Oct 24 2013, 07:02:20) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin 

$PATH設置爲

/Users/mingot/soft/anaconda/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/texbin:/opt/X11/bin 

在虛擬環境裏面,Python版本是:

(env)$ python 
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin 

$PATH

/Users/mingot/virtualenvs/env/bin:/Users/mingot/soft/anaconda/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/texbin:/opt/X11/bin 

我明白,問題是,在虛擬環境中,在執行非水蟒蟒2.7.5,它依然使用platforms.py從蟒蛇庫,導致崩潰的評價正則表達式建議爲here。我不關心在虛擬環境中使用哪個python版本。任何關於如何告訴內部虛擬環境的Python使用正確的platforms.py的建議?

謝謝!

回答

7

我在安裝了Anaconda的Mac上遇到了同樣的問題。我的方式固定它在尋找platform.py,然後修改以下(註釋掉線):

ORIGINAL

_sys_version_parser = re.compile(
    r'([\w.+]+)\s*' 
    '\|[^|]*\|\s*' # version extra 
    '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*' 
    '\[([^\]]+)\]?') 

CHANGE TO

_sys_version_parser = re.compile(
    r'([\w.+]+)\s*' 
    #'\|[^|]*\|\s*' # version extra # Just comment this line out 
    '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*' 
    '\[([^\]]+)\]?') 

這是用的virtualenv == 1.10.1與蟒蛇1.9.2

+0

感謝威爾,我最終會做相同的! – kahlo

+0

這也適用於我。 – HorseloverFat

3

的解決方案我最終做的是創造從正確的Python版本的庫文件的虛擬環境的庫文件夾中的符號鏈接platforms.py

$ pwd 
/Users/mingot/virtualenvs/env/lib/python2.7 
$ ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/platform.py platform.py 
2

我摸索出的解決方案是爲s因此它不僅可以將Python二進制文件複製到新環境中,而且還可以附帶共享庫文件libpythonX.Y.so。沒有它們,小型的Anaconda Python二進制文件(看一下它 - 只有幾千字節大小)鏈接到系統libpython而不是Anaconda libpython,因此您可以通過Anaconda的突變體platform.py代碼解析系統Python的版本字符串。

下面是一個簡短的shell腳本將修復virtualenv.py;運行它,一旦你有你的蟒蛇環境激活並已運行pip install virtualenv,使它的virtualenv工具可用的內部:

https://github.com/brandon-rhodes/homedir/blob/master/bin/%2Cfix-virtualenv-for-anaconda