Installing virtualenv under Anaconda on OSX

来源:互联网 发布:java异步调用存储过程 编辑:程序博客网 时间:2024/04/29 17:01

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/pythonInstalling 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 MAXREPEATImportError: 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/pythonbin/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.

原创粉丝点击