本文介绍了OS X上的Python 2.6是否应该在$ PYTHONPATH中处理多个easy-install.pth文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 sage 运行ipython,并且还使用了一些不在鼠尾草(lxml ,argparse)安装在我的主目录中.因此,我最终获得了

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

Python正在读取并处理它找到的第一个easy-install.pth($ HOME/sage/local/lib/python/site-packages/easy-install.pth),但不是第二个,因此鸡蛋已安装在$ HOME中/lib/python没有添加到路径中.在阅读现成的site.py时,我一生都无法理解为什么这样做.

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.

有人可以启发我吗?还是建议如何推动Python读取两个easy-install.pth文件?

Can someone enlighten me? Or advise how to nudge Python into reading both easy-install.pth files?

将两者合并到一个.pth文件中是目前可行的解决方法,因此此问题主要是出于好奇心的考虑.

Consolidating both into one .pth file is a viable workaround for now, so this question is mostly for curiosity value.

推荐答案

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

TL;DR: call site.addsitedir to process .pth files

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

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"

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

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.

现在,site.module所做的实际上是使用(sys.exec_prefix,sys.prefix)和依赖于操作系统的前缀(对于Linux:lib/python2.7/dist-packages)的组合,检查是否有任何组合是现有目录,并进行处理(解析包括的.pth文件)

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)

代码在site.py模块中-getsitepackages().

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

此函数最终返回一个列表,并为该列表的每个元素调用addsitedir函数-在该函数中,您具有使.pth文件正常工作的逻辑.

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.

从长远来看-处理.pth文件很短-在您的入门级脚本中调用site.addistedir.您还可以考虑将它放在sitecustomize.py中-只需确保您的python发行版还没有它.

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.

这篇关于OS X上的Python 2.6是否应该在$ PYTHONPATH中处理多个easy-install.pth文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:11