Homebrew Python如何写入/Library/Python/2.7/site-packages/目录?

8

有没有一份权威的指南,教我们如何正确地安装Homebrew的Python,以及pip、virtualenv、virtualenvwrapper等工具?

我已经阅读了以下页面:

https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python
http://osxastrotricks.wordpress.com/2012/05/02/python-setup-with-homebrew/
http://skipperkongen.dk/2011/11/13/installing-django-in-virtual-environment-on-mac/
http://www.thisisthegreenroom.com/2011/installing-python-numpy-scipy-matplotlib-and-ipython-on-lion/
http://youshoulddoityourself.blogspot.com.au/2010/11/test.html

我有OSX 10.8(Mountain Lion)DP3,并安装了XCode命令行工具。 我还安装了XQuartz,以防万一(10.8不提供它)。 然后我运行了:
brew install python  --universal --framework

我将以下内容添加到了我的~/.bash_profile中:
export PATH="/usr/local/bin:/usr/local/share/python:${PATH}"

我随后运行了:

sudo easy_install pip

这似乎是有效的。

我检查了pip的安装位置:

 Victors-MacBook-Pro:~ victorhooi$ which pip
/usr/local/bin/pip

然而,当我尝试使用pip安装软件包时,出现了一个错误,指出无法写入到/Library/Python/2.7/site-packages:

running install_lib

creating /Library/Python/2.7/site-packages/yolk

error: could not create '/Library/Python/2.7/site-packages/yolk': Permission denied

----------------------------------------
Command /usr/bin/python -c "import  setuptools;__file__='/Users/victorhooi/build/yolk/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/gg/m37t1rkx4zj7z54ls487jfc40000gn/T/pip-ap7LSB-record/install-record.txt failed with error code 1 in /Users/victorhooi/build/yolk
Storing complete log in /Users/victorhooi/Library/Logs/pip.log

我的理解是,Homebrew的Python会安装到它自己的site-packages中,我不需要在整个文件系统树上进行奇怪的chown操作才能使它正常工作?

谢谢, 维克多


我在安装了 Mountain Lion 的新系统后遇到了同样的问题。我曾经有一个类似的问题(https://dev59.com/oWXWa4cB1Zd3GeqPR_bD),我自己回答了这个问题。我的问题中有一节讲述如何修改 /System 中的符号链接,使其指向 brew 目录,但这似乎是一个 hack。希望有人能回答你的问题。 - Hassan Syed
3个回答

10

与此同时,Homebrew 的 Python 已经发生了变化:

  • 通过 Homebrew 安装的 Python 不再尝试写入 /Library/Python
  • Homebrew 的 Python 现在始终使用自己的 site-packages。
  • pip(和 distribute)会自动安装。
  • 不再需要使用 "--framework" 选项。 Homebrew 现在默认为它。
  • 由于 Tk headers 链接到 X11 headers,因此需要 XQuartz(不幸的是)。

.bashrc 中的 PATH 是正确的(并且仍然需要)。


假设我不关心Tk。在这种情况下,有没有办法不安装X11? - bwinton
很快就会有。请参见该问题 - Samuel John
1
我提出的那个问题?是的,我一直在关注它。 :) (顺便说一句,谢谢你对此所做的工作!) - bwinton
1
“Homebrew的Python现在总是使用它自己的site-packages。”- 这正是我所期望的,但这并不是我所看到的(https://dev59.com/xobca4cB1Zd3GeqPb-mb)。 - orome

2
请相信我,作为一个曾经和你一样的人,先使用虚拟环境,再使用pip和其他工具。它们非常好用,可以解决当前和未来的许多问题。

哎呀,等等,我正在使用虚拟环境困惑。我不是在第一行提到了它们吗?我的正常设置是使用easy_install安装pip,然后使用pip安装virtualenv/virtualenvwrapper - 我通常还会加入yolk,这样我就可以看到安装了什么。 - victorhooi
1
尝试使用 easy_install > virtualenv > pip > yolk 进行安装。在安装后激活 venv。 - nathancahill

-1

现在有了Mountain Lion 10.8.2,而我之前从Lion遗留下来的Python 2.7安装就出了问题。无论我尝试什么,Python都想要访问/Library/..这不好。简单的解决方案是brew uninstall python然后再brew install python。这基本上解决了所有与homebrew在ML上相关的Python问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接