Installing virtualenv under Anaconda on OSX

OS X 10.8 by default comes with python 2.7.2.

For using iPython Notebook (http://ipython.org/notebook.html) I usually install anaconda (http://continuum.io/downloads) in my home dir right away so I have a python 2.7.5 at ~/anaconda/bin/python. Anaconda installer puts itself in front of the system /usr/bin/python in my $PATH variable so running "python" would be calling the anaconda version of python straight away.

When I try to create a virtualenv, I once see this problem:

$ virtualenv pg/
New python executable in pg/bin/python
Installing Setuptools...................
  Complete output from command /Users/ryan/tmp/python/pg/bin/python setup.py install --single-version-externally-managed --record record:
  Traceback (most recent call last):
  File "setup.py", line 5, in <module>
    import textwrap
  File "/Users/ryan/anaconda/lib/python2.7/textwrap.py", line 10, in <module>
    import string, re
  File "/Users/ryan/anaconda/lib/python2.7/string.py", line 83, in <module>
    import re as _re
  File "/Users/ryan/tmp/python/pg/lib/python2.7/re.py", line 105, in <module>
    import sre_compile
  File "/Users/ryan/tmp/python/pg/lib/python2.7/sre_compile.py", line 14, in <module>
    import sre_parse
  File "/Users/ryan/tmp/python/pg/lib/python2.7/sre_parse.py", line 17, in <module>
    from sre_constants import *
  File "/Users/ryan/tmp/python/pg/lib/python2.7/sre_constants.py", line 18, in <module>
    from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT

After some searching I found this link: http://stackoverflow.com/questions/16301735/importerror-cannot-import-name-maxrepeat-with-cx-freeze . Seems to be I'm installing an earlier version of python inside the virtualenv just created. By looking at the md5 of virtualenv installed bin/python binary it is the same as the anaconda binary, but when entering the interactive shell, it shows python 2.7.2, same as the system python. 

By running otool -L pg/bin/python the results are:

$ otool -L bin/python
bin/python:
	@loader_path/../lib/libpython2.7.dylib (compatibility version 2.7.0, current version 2.7.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 476.0.0)
	/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)

and there is no "libpython2.7.dylib" under lib/ inside the virtualenv, OS X loader falls back to the /usr/lib directory and found the libpython2.7.dylib there, which is the 2.7.2 version with an built-in _sre module missing the MAXREPEAT variable given above, hence the error.

Solutions:

Set DYLD_LIBRARY_PATH (for OSX, or LD_LIBRARY_PATH on Linux if needed) to search Anaconda's lib/ directory before the system.

Or, better off use "conda" recommended by Anaconda / iPython. When creating an environment using "conda create", all required libraries (.dylib files) are copied into the lib/ dir under the environment, which is then loaded correctly by the interpreter without problem.

http://docs.continuum.io/conda/index.html#getting-started

Disclaimer:

Not sure if this only happens with python installed by Anaconda. Didn't see this in a previous installation of OSX, or on any of my LInux servers though.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值