【问题标题】:Why do include paths in python2 and python3 differ?为什么 python2 和 python3 中的包含路径不同?
【发布时间】:2018-02-16 11:47:49
【问题描述】:

在尝试使基于 scons 的构建系统尽可能独立于平台时,我想知道以下几点:

为什么python2返回包含路径/usr/local/include/python2.7?该路径不包含Python.h,如果我依赖该路径,构建将失败。

python2

在 python2 中使用 sysconfig:

$ /usr/bin/python2
Python 2.7.13 (default, Nov 23 2017, 15:37:09) 
[GCC 6.3.0 20170406] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_path('include')
'/usr/local/include/python2.7'

/usr/local/include/python2.7。这是一个空文件夹。

从 shell 调用 python2-config:

$ /usr/bin/python2-config --includes
-I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7

这给出了不同的路径。我能够在/usr/include/python2.7 中找到Python.h

python3

在 python3 中使用 sysconfig:

$ /usr/bin/python3
Python 3.5.3 (default, Nov 23 2017, 11:34:05) 
[GCC 6.3.0 20170406] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_path('include')
'/usr/include/python3.5m'

从 shell 调用 python3-config:

/usr/bin/python3-config --includes
-I/usr/include/python3.5m -I/usr/include/python3.5m

得到的路径/usr/include/python3.5m 与小船进近相同。 Python.h 位于此处。

蟒蛇

如果我使用 anaconda python(2 或 3),路径也是一致的(与 python3 一样)。

我已经找到了一些解决方法,例如在usr/local/include 中放置一个指向usr/include 的软链接,或者只是丢弃路径中的local/,但两者看起来都不是一个好的解决方案。

编辑:

目前,python2 中不正确的包含路径使我的构建系统不像我想要的那样独立于平台。如果使用 python2,添加(可选)环境变量 PYTHON_INCLUDE_PATH 可以让我定义正确的路径。但是始终返回正确路径的解决方案会对我有很大帮助(在 python 端或使用 scons 特定的功能)。 因为我的构建系统是基于 scons a

【问题讨论】:

  • 为什么 scons 被标记?
  • 我的构建系统是基于 scons 的。如果有(特定于 scons 的)方法来获取正确的包含路径,这也会有很大帮助。
  • 什么版本的 SCons 以及你使用哪个版本的 python 运行它?如果您需要帮助,请发布您的相关 SCons 逻辑 (SConstruct/SConscript)..
  • 真正让我困惑的是上面描述的 python2-config 与 sysconfig.get_path('include') 的奇怪行为。如果有一个 scons 特定的方法来规避这个问题,它也可以解决我的问题,但它并没有真正回答这个问题。所以你是对的,scons 标签有点误导。明天我将打开另一个更具体的问题。感谢您迄今为止的帮助!
  • 感谢 Jeff 的回答,不再需要 scons 特定的解决方案。谢谢!

标签: python-3.x python-2.7 include-path


【解决方案1】:

Python 使用不同的安装方案,具体取决于 平台和安装选项。

https://docs.python.org/2/library/sysconfig.html#installation-paths

有多种方案,包括posix_localposix_prefix,它们确定了各种安装目录的位置。似乎sysconfig 实际上并没有记录用于安装特定 Python 构建的方案——它只有构建信息。

因此,当您调用sysconfig.get_path() 时,它会根据当前平台的默认值来猜测方案 [1]。 Python2.7 sysconfig 猜测 posix_local 而 Python3 sysconfig 猜测 posix_prefix [2]。

看起来两个版本的 Python 都是使用 posix_prefix 方案安装的,所以你可以在调用 sysconfig.get_path 时指定:

$ python -c "import sysconfig; print(sysconfig.get_path('include', 'posix_prefix'))"
/usr/include/python2.7

$ python3 -c "import sysconfig; print(sysconfig.get_path('include', 'posix_prefix'))"
/usr/include/python3.5m

[1]https://github.com/python/cpython/blob/2.7/Lib/sysconfig.py#L169

[2] 将sysconfig 作为脚本运行:

$ python -m sysconfig | head
Platform: "linux-x86_64"
Python version: "2.7"
Current installation scheme: "posix_local"

$ python3 -m sysconfig | head
Platform: "linux-x86_64"
Python version: "3.5"
Current installation scheme: "posix_prefix"

我实际上在sysconfig 源中找不到posix_local,所以不确定它来自哪里。

编辑

我对此进行了更多研究,并了解到它特定于 Debian/Ubuntu 版本的 Python;上游 Python 不使用 /usr/local/posix_local 方案。 Debian 软件包使用一种混合方法,与posix_prefix 相同,只是为模块添加了/usr/local/

我没有在网上找到源链接,但是我的本地系统在其 Python2.7 sysconfig.py 中有这个链接(注意FIXME):

def _get_default_scheme():
    if os.name == 'posix':
        # the default scheme for posix on Debian/Ubuntu is posix_local
        # FIXME: return dist-packages/posix_prefix only for
        #   is_default_prefix and 'PYTHONUSERBASE' not in os.environ and 'real_prefix' not in sys.__dict__
        # is_default_prefix = not prefix or os.path.normpath(prefix) in ('/usr', '/usr/local')
        return 'posix_local'
    return os.name

Debian python3 sysconfig.py 取消了 posix_local 并使用与上游 python 相同的默认值:

def _get_default_scheme():
    if os.name == 'posix':
        # the default scheme for posix is posix_prefix
        return 'posix_prefix'
    return os.name

您可能希望复制它以与 Mac 或 Windows 兼容:

sysconfig.get_path('include', 'posix_prefix' if os.name == 'posix' else os.name)

https://wiki.debian.org/Python#Deviations_from_upstream
https://www.debian.org/doc/packaging-manuals/python-policy/python.html#paths

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多