OS X 上的 Python 2.6 是否应该处理 $PYTHONPATH 中的多个 easy-install.pth 文件?
我正在从 sage 运行 ipython,并且还使用了一些不在 sage 中的软件包(lxml、argparse)它们安装在我的主目录中。因此我最终得到了 $PYTHONPATH
$HOME/sage/local/lib/python:$HOME/lib/python
Python 正在读取并处理它找到的第一个 easy-install.pth ($HOME/sage/local/lib/python/site-packages/easy-install .pth),但不是第二个,因此安装在 $HOME/lib/python 中的 Egg 不会添加到路径中。在阅读现成的 site.py 时,我无法理解它为什么要这样做。
有人可以启发我吗?或者建议如何推动 Python 读取两个 easy-install.pth 文件?
目前,将两者合并到一个 .pth 文件中是一种可行的解决方法,因此这个问题主要是为了好奇。
I am running ipython from sage and also am using some packages that aren't in sage (lxml, argparse) which are installed in my home directory. I have therefore ended up with a $PYTHONPATH of
$HOME/sage/local/lib/python:$HOME/lib/python
Python is reading and processing the first easy-install.pth it finds ($HOME/sage/local/lib/python/site-packages/easy-install.pth) but not the second, so eggs installed in $HOME/lib/python aren't added to the path. On reading the off-the-shelf site.py, I cannot for the life of me see why it's doing this.
Can someone enlighten me? Or advise how to nudge Python into reading both easy-install.pth files?
Consolidating both into one .pth file is a viable workaround for now, so this question is mostly for curiosity value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TL;DR:调用 site.addsitedir 来处理 .pth 文件
我不确定 OS X,但 PYTHONPATH 和 site 包实际上在增强 sys.path 方面是独立的。
在我的 Linux 机器上,PYTHONPATH 两次都是输出的一部分 - 尽管第二次运行中的 -S 开关会跳过导入站点模块。
现在,site.module 所做的实际上是采用(sys.exec_prefix、sys.prefix)和操作系统相关前缀(对于 linux:lib/python2.7/dist-packages)的组合,检查任何组合是否是现有目录,如果是这样处理它(包括解析 .pth 文件)
代码位于 site.py 模块 - getsitepackages() 中。
该函数最终返回一个列表,并为该列表的每个元素调用 addsitedir 函数 - 在该函数中,您拥有使 .pth 文件正常工作的逻辑。
长话短说 - 要处理 .pth 文件 - 在入门级脚本中调用 site.addistedir 。您也可以考虑将其添加到您的 sitecustomize.py 中 - 只要确保您的 python 发行版中还没有它即可。
TL;DR: call site.addsitedir to process .pth files
I'm not sure about OS X, but PYTHONPATH and site package are actually kind of independent where it comes to augmenting sys.path.
On my linux box, the PYTHONPATH is part of the output both times - even though -S switch in the second run skips importing the site module.
Now, what site.module does is actually taking combinations of (sys.exec_prefix, sys.prefix) and OS dependant prefixes (for linux: lib/python2.7/dist-packages), checks if any of the combinations is an existing directory, and if so processes it (parsing .pth files included)
The code is in site.py module - getsitepackages().
This function eventually returns a list, and for each element of that list addsitedir function is called - and in that one, you have the logic to get .pth files working.
So long story short - to process .pth files - call site.addistedir in your entry-level script. You might also consider having it in your sitecustomize.py - just be sure your python distribution does not already have one.