python - 一种在 Linux 和 OS X 操作系统上获取用户安装包路径的方法? (适用于 2.5 - 2.7 之间的 Python 版本)

标签 python linux macos path python-2.x

>>> 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 的操作系统)

最佳答案

如果您需要get_python_lib 函数的特定功能,source for that module相当简单,根本不使用任何 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 patches this function在默认情况下返回 dist-packages,这不适用于 OS X。

关于python - 一种在 Linux 和 OS X 操作系统上获取用户安装包路径的方法? (适用于 2.5 - 2.7 之间的 Python 版本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12227084/

相关文章:

linux - 在Windows 10中安装Ubuntu 15.10

c - 如何在 Linux 上以编程方式设置硬件时钟?

python - 如何获取Python类中的静态函数字段

python - Django 多态树 list_display 内容字段中的对象名称

python - Django-NVD3 - 无法更改线条颜色

linux - 关于 Linux 上 CUDA C 的程序 "Hello world"

python - Pandas 面板最大

objective-c - 应用程序中可能使用的嵌入式数据库引擎

macos - 自动检测 USB 设备连接/断开

iphone - Mac 中的 Git 问题( fatal error )