安装Homebrew Python后,Python包在错误的位置? [英] Python packages in wrong location after installing Homebrew Python?
问题描述
将Homebrew的Python安装到具有已建立的Apple Python的系统上之后,sys.path
使用Homebrew的Python列出的最后一项是
After installing Homebrew's Python onto a system with an established Apple Python, the last entries listed by sys.path
using Homebrew's Python are
/Library/Python/2.7/site-packages
/usr/local/lib/python2.7/site-package
这与我期望的顺序相反.不应该首先搜索Homebrew的软件包吗? (实际上,应该不是仅搜索 的地方吗?)这就是文档中暗示的内容.设置在哪里,我(或应该如何)更改它?
This is the reverse of the order I expect. Shouldn't Homebrew's packages be searched first? (In fact, shouldn't it be the only place searched?) That's what's implied in the documentation. Where is it set and how can I (or should I) change it?
或者这是Brewed Python应该工作的方式吗?它是否旨在防止重复的软件包,并假定系统site-packages
中的任何软件包都应保留在该目录中,除非明确卸载并随后安装(进入Brew的目录);除了pip
和setuptools
重复的 (并放在Brewed Python的路径中)之外.
Or is this the way Brewed Python is supposed to work? Is it designed to prevent duplicate packages and assume that any package in the system site-packages
is meant to stay there unless explicitly uninstalled and then subsequently installed (into Brew's); with the exception of pip
and setuptools
which are duplicated (and put first in Brewed Python's path).
推荐答案
这是预期的行为.其基本原理是,尽管您现在正在使用新的自制Python,但仍可以继续使用旧的已安装模块.
That is the intended behaviour. The rationale behind it is that you can keep using your old installed modules despite the fact that you are now using a new homebrewed Python.
现在这有一些缺点,例如某些库,例如numpy,将无法在不同的Python版本上运行,因此,如果您安装了numpy,它将从旧系统的site-packages
导入,并且将无法工作.
Now this has some drawbacks, for example some libraries like numpy, won't work across different Python versions, so if you had installed numpy it will be imported from the old system's site-packages
and won't work.
至少有两种方法可以更改sys.path
:
There are at least two ways to change sys.path
:
使用.pth
文件:
Use a .pth
file:
Python将从某些内置位置(例如:〜/Library/Python/2.7/lib/python/site-packages/homebrew.pth)中进行选择.这附加到sys.path
上,这并不理想,但是具有Python 3不会选择的优点.它当前是推荐方法.您可以使用以下方法实现此目标:
Python will pick that from some of the builtin locations (ex: ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth). This appends to sys.path
which is not ideal but has the advantage that it won't be picked by Python 3. It is currently the recommended method. You can achieve this with:
echo "$(brew --prefix)/lib/python2.7/site-packages" > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
设置PYTHONPATH
:
Set PYTHONPATH
:
这是sys.path
的前缀,它的缺点是所有python版本都是全局的,因此,如果要使用其他python版本,则不建议这样做.您可以通过添加到您的.bash_profile
:
This gets prepended to sys.path
, it has the drawback that is global to all python versions so it is not recommended if you are going to use different python versions. You can do it by adding to your .bash_profile
:
export PYTHONPATH=`brew --prefix`/lib/python2.7/site-packages:$PYTHONPATH
我个人将选项2与homebrew-python一起使用(我现在使用并推荐Anaconda).我的理由是我当时并不关心系统的Python或Python 3.
I personally used option 2 with homebrew-python (I now use and recommend Anaconda). My reasons were that I didn't care about system's Python or Python 3 at the time.
这篇关于安装Homebrew Python后,Python包在错误的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!