OS X 上的 Python 2.6 是否应该处理 $PYTHONPATH 中的多个 easy-install.pth 文件?

发布于 2024-08-31 23:28:01 字数 544 浏览 3 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2024-09-07 23:28:01

TL;DR:调用 site.addsitedir 来处理 .pth 文件


我不确定 OS X,但 PYTHONPATH 和 site 包实际上在增强 sys.path 方面是独立的。

Try this:
    set PYTHONPATH somehow (OS dependent)
    python -c "import sys; print '\n'.join(sys.path); print sys.exec_prefix; print sys.prefix"
    python -S -c "import sys; print '\n'.join(sys.path);print sys.exec_prefix; print sys.prefix"

在我的 Linux 机器上,PYTHONPATH 两次都是输出的一部分 - 尽管第二次运行中的 -S 开关会跳过导入站点模块。

现在,site.module 所做的实际上是采用(sys.exec_prefix、sys.prefix)和操作系统相关前缀(对于 linux:lib/python2.7/dist-packages)的组合,检查任何组合是否是现有目录,如果是这样处理它(包括解析 .pth 文件)

代码位于 site.py 模块 - getsitepackages() 中。

def getsitepackages():
    """Returns a list containing all global site-packages directories
    (and possibly site-python).

    For each directory present in the global ``PREFIXES``, this function
    will find its `site-packages` subdirectory depending on the system
    environment, and will return a list of full paths.
    """
    sitepackages = []
    seen = set()

    for prefix in PREFIXES:
        if not prefix or prefix in seen:
            continue
        seen.add(prefix)

        if sys.platform in ('os2emx', 'riscos'):
            sitepackages.append(os.path.join(prefix, "Lib", "site-packages"))
        elif os.sep == '/':
            sitepackages.append(os.path.join(prefix, "lib",
                                        "python" + sys.version[:3],
                                        "site-packages"))
            sitepackages.append(os.path.join(prefix, "lib", "site-python"))
        else:
            sitepackages.append(prefix)
            sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
        (...)

该函数最终返回一个列表,并为该列表的每个元素调用 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.

Try this:
    set PYTHONPATH somehow (OS dependent)
    python -c "import sys; print '\n'.join(sys.path); print sys.exec_prefix; print sys.prefix"
    python -S -c "import sys; print '\n'.join(sys.path);print sys.exec_prefix; print sys.prefix"

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().

def getsitepackages():
    """Returns a list containing all global site-packages directories
    (and possibly site-python).

    For each directory present in the global ``PREFIXES``, this function
    will find its `site-packages` subdirectory depending on the system
    environment, and will return a list of full paths.
    """
    sitepackages = []
    seen = set()

    for prefix in PREFIXES:
        if not prefix or prefix in seen:
            continue
        seen.add(prefix)

        if sys.platform in ('os2emx', 'riscos'):
            sitepackages.append(os.path.join(prefix, "Lib", "site-packages"))
        elif os.sep == '/':
            sitepackages.append(os.path.join(prefix, "lib",
                                        "python" + sys.version[:3],
                                        "site-packages"))
            sitepackages.append(os.path.join(prefix, "lib", "site-python"))
        else:
            sitepackages.append(prefix)
            sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
        (...)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文