为什么python2和python3中的包含路径不同?

时间:2018-02-16 11:47:50

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

在尝试使我的基于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

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

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对于boath方法是相同的。 Python.h位于此处。

水蟒

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

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

编辑:

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

1 个答案:

答案 0 :(得分: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]。

看起来使用posix_prefix方案安装了两个版本的Python,因此您可以在调用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"

我无法在posix_local来源中找到sysconfig,因此无法确定其来源。

修改

我对此进行了一些研究,并了解到它特定于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

相关问题