0
>>> import distutils.sysconfig
>>> distutils.sysconfig.get_python_lib()
/usr/lib/python2.7/dist-packages

但我想要这条路:/usr/local/lib/python2.7/dist-packages. 我无法使用该sysconfig模块,因为它仅在 python2.7 上受支持,并且该模块没有 PyPI 下载。所以我什至不能使用需求文件让其他用户用 pip 下载它。

那么,有什么方法可以获取适用于 Linux 和 OS X 操作系统的已安装软件包的路径吗?(甚至所有基于 Unix 的操作系统)

4

2 回答 2

1

如果您需要函数的特定功能,该模块的源代码get_python_lib非常简单,根本不使用任何 Python 2.7 特定语法;你可以简单地向后移植它。

您基本上需要以下定义和两个功能:

import os
import sys
from distutils.errors import DistutilsPlatformError


PREFIX = os.path.normpath(sys.prefix)
EXEC_PREFIX = os.path.normpath(sys.exec_prefix)


def get_python_version():
    """Return a string containing the major and minor Python version,
    leaving off the patchlevel.  Sample return values could be '1.5'
    or '2.2'.
    """
    return sys.version[:3]

def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
    """Return the directory containing the Python library (standard or
    site additions).

    If 'plat_specific' is true, return the directory containing
    platform-specific modules, i.e. any module from a non-pure-Python
    module distribution; otherwise, return the platform-shared library
    directory.  If 'standard_lib' is true, return the directory
    containing standard Python library modules; otherwise, return the
    directory for site-specific modules.

    If 'prefix' is supplied, use it instead of sys.prefix or
    sys.exec_prefix -- i.e., ignore 'plat_specific'.
    """
    if prefix is None:
        prefix = plat_specific and EXEC_PREFIX or PREFIX

    if os.name == "posix":
        libpython = os.path.join(prefix,
                                 "lib", "python" + get_python_version())
        if standard_lib:
            return libpython
        else:
            return os.path.join(libpython, "site-packages")

    elif os.name == "nt":
        if standard_lib:
            return os.path.join(prefix, "Lib")
        else:
            if get_python_version() < "2.2":
                return prefix
            else:
                return os.path.join(prefix, "Lib", "site-packages")

    elif os.name == "os2":
        if standard_lib:
            return os.path.join(prefix, "Lib")
        else:
            return os.path.join(prefix, "Lib", "site-packages")

    else:
        raise DistutilsPlatformError(
            "I don't know where Python installs its library "
            "on platform '%s'" % os.name)

当然,您可以将长函数缩减为您的平台所需的分支;对于 OS X 那将是:

def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
    if prefix is None:
        prefix = plat_specific and EXEC_PREFIX or PREFIX

    libpython = os.path.join(prefix,
                             "lib", "python" + get_python_version())
    if standard_lib:
        return libpython
    else:
        return os.path.join(libpython, "site-packages")

请注意,Debian修补此函数以在默认情况下返回dist-packages,这不适用于 OS X。

于 2012-09-01T12:02:37.403 回答
0

如果你想要路径,你可以使用 sys.path:

import sys
print sys.path

如果要配置它,可以设置环境变量 PYTHON_PATH:

export PYTHON_PATH=/usr/local/lib/python2.7/dist-packages

如果您想在不更改 PYTHON_PATH 的情况下动态添加它,也可以在 python 中扩展它:

import sys
sys.path.extend(['/usr/local/lib/python2.7/dist-packages'])
于 2012-09-01T11:09:07.360 回答