在OSX上使用easy_install或setup.py安装Python模块
我正在使用Snow Leopard 10.6系统,想要安装以下几个Python模块:
- numpy
- scipy
- matplotlib
我遇到了一些问题,因为OSX里有两个版本的Python:
- /Library/Python/
- /System/Library/Frameworks/Python.framework/
当我执行以下命令时:
sudo easy_install -U {module}
,模块似乎被安装到了这个目录:
bash-3.2$ ls -al /Library/Python/2.6/site-packages/
total 688
drwxrwxr-x 12 root admin 408 Aug 24 23:26 .
drwxrwxr-x 3 root admin 102 Feb 11 2010 ..
-rw-rw-r-- 1 root admin 119 Feb 11 2010 README
-rw-r--r-- 1 root admin 267 Aug 24 19:03 easy-install.pth
drwxr-xr-x 5 root admin 170 Aug 24 10:42 nose-0.11.4-py2.6.egg
drwxr-xr-x 38 root admin 1292 Aug 24 15:35 numpy
-rw-r--r-- 1 root admin 1618 Aug 24 15:35 numpy-2.0.0.dev8661-py2.6.egg-info
drwxr-xr-x 16 root admin 544 Aug 24 19:07 numscons
drwxr-xr-x 4 root admin 136 Aug 24 19:03 numscons-0.10.1-py2.6.egg
-rw-r--r-- 1 root admin 265 Aug 24 19:07 numscons-0.12.0dev-py2.6.egg-info
-rw-r--r-- 1 root admin 333959 Aug 23 11:51 setuptools-0.6c11-py2.6.egg
-rw-r--r-- 1 root admin 30 Aug 23 11:51 setuptools.pth
但是,当我尝试安装scipy时,出现了以下错误:
config = setup_module.configuration(*args)
File "scipy/setup.py", line 20, in configuration
config.add_subpackage('special')
File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/numpy/distutils/misc_util.py", line 851, in add_subpackage
caller_level = 2)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/numpy/distutils/misc_util.py", line 834, in get_subpackage
caller_level = caller_level + 1)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/numpy/distutils/misc_util.py", line 766, in _get_configuration_from_setup_py
('.py', 'U', 1))
File "scipy/special/setup.py", line 14, in <module>
(numpy.__version__, numpy.__file__))
ValueError: numpy >= 1.4 is required (detected 1.2.1 from /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/numpy/__init__.pyc)
看起来它在我的框架目录中寻找一个旧版本的numpy。我用import命令查看Python找到的numpy版本:
python -c 'import numpy;print numpy.__version__'
1.2.1
果然,它在框架目录中查找,尽管我在这里有一个新版本:
/Library/Python/2.6/site-packages/
我知道import命令会先在本地目录查找,然后再去PYTHONPATH,最后才看sys.path。所以我检查了一下这些,发现我现在没有设置PYTHONPATH,这里是我的sys.path:
/Library/Python/2.6/site-packages/setuptools-0.6c11-py2.6.egg
/Library/Python/2.6/site-packages/nose-0.11.4-py2.6.egg
/Library/Python/2.6/site-packages/numscons-0.10.1-py2.6.egg
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/Library/Python/2.6/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode
如果我把PYTHONPATH改成/Library/Python/2.6/site-packages
,然后检查numpy的版本,我就能得到正确的版本:
bash-3.2$ python -c 'import numpy; print numpy.__version__'
2.0.0.dev8661
但是当我运行sudo python setup.py build/install
时,scipy还是找不到正确的numpy,尽管PYTHONPATH已经设置了。
有没有人能帮我解决这个问题?
我找到一个链接,看起来可以解决我的问题,但我似乎无法让它工作:
http://andreasjacobsen.com/2008/10/10/using-python-setuptools-on-the-mac/
6 个回答
我想出的解决办法如下。
1) 不要使用OSX自带的Python版本。苹果对这个版本做了一些修改,导致并不是所有东西都能正常运行。所以,你需要安装一个开发版的Python。目前我建议安装Python 2.7。这里有一篇博客介绍怎么安装:
https://medium.com/cs-math/a3eb146ebfb5
2) 安装完Python后,你还需要安装Scipy和Numpy所需的fortran库。可以用homebrew来安装(现在还有人用MacPorts吗??)
3) 安装好homebrew后,接着安装fortran。
brew install gfortran
4) 现在你可以用pip成功安装scipy和numpy了(注意,这个过程可能需要一些时间)。
pip install -U numpy
pip install -U scipy
完成!