首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否覆盖Python setuptool的默认include_dirs和library_dirs?

是否覆盖Python setuptool的默认include_dirs和library_dirs?
EN

Stack Overflow用户
提问于 2018-07-25 23:08:07
回答 1查看 1.1K关注 0票数 2

我在setup.py中指定了以下include_dirslibrary_dirs

代码语言:javascript
复制
/opt/x86_64-sdk-linux/usr/bin/python3 setup.py build_ext \
--include-dirs=/opt/neon-poky-linux-gnueabi/usr/include/python3.5m/ \
--library-dirs=/opt/neon-poky-linux-gnueabi/usr/lib/ \
--rpath=/opt/neon-poky-linux-gnueabi/usr/lib/ \
--plat-name=linux_armv7l

但是,生成的gcc命令(在执行python3 setup.py build_ext时)还包括运行python3的include路径(为了可读性,我添加了换行符):

代码语言:javascript
复制
arm-poky-linux-gnueabi-gcc \
--sysroot=/opt/neon-poky-linux-gnueabi \
-I. \
-I/opt/neon-poky-linux-gnueabi/usr/include/python3.5m/ \
-I/opt/x86_64-sdk-linux/usr/include/python3.5m \
-c py/constraint.cpp -o build/temp.linux-x86_64-3.5/py/constraint.o

第三个包含路径未显式指定,但在编译时仍在使用。

我该如何确保只使用我指定的include-dirs

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-26 03:41:57

您将需要覆盖build_ext命令,因为the stdlib's build_ext ensures the Python header files, both platform specific and not, are always appended to the include paths

以下是自定义build_ext命令的示例,该命令在最终确定选项后清理包含路径:

代码语言:javascript
复制
# setup.py

from distutils import sysconfig
from setuptools import setup
from setuptools.command.build_ext import build_ext as build_ext_orig


class build_ext(build_ext_orig):

    def finalize_options(self):
        super().finalize_options()
        py_include = sysconfig.get_python_inc()
        plat_py_include = sysconfig.get_python_inc(plat_specific=1)
        for path in (py_include, plat_py_include, ):
            for _ in range(self.include_dirs.count(path)):
                self.include_dirs.remove(path)


setup(
    ...,
    cmdclass={'build_ext': build_ext},
)

更新

库目录的方法相同:当选项最终确定时,清除列表:

代码语言:javascript
复制
class build_ext(build_ext_orig):

    def finalize_options(self):
        super().finalize_options()
        ...
        libdir = sysconfig.get_config_var('LIBDIR')
        for _ in range(self.library_dirs.count(libdir)):
            self.library_dirs.remove(libdir)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51522248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档