如何在调用 setup.py 安装时将 --debug 传递给 build_ext?

How to pass --debug to build_ext when invoking setup.py install?

当我执行命令 python setup.py installpython setup.py develop 时,它会执行 build_ext 命令作为步骤之一。如何将 --debug 选项传递给它,就好像它被调用为 python setup.py build_ext --debug?

更新 这是一个与我的非常相似的 setup.pyhttps://github.com/pybind/cmake_example/blob/11a644072b12ad78352b6e6649db9dfe7f406676/setup.py#L43

我想调用 python setup.py install 但将 debug 属性 in build_ext class 实例变为 1.

您可以使用类似下面的方法来完成

from distutils.core import setup
from distutils.command.install import install
from distutils.command.build_ext import build_ext

class InstallLocalPackage(install):
    def run(self):
        build_ext_command = self.distribution.get_command_obj("build_ext")
        build_ext_command.debug = 1
        build_ext.run(build_ext_command)
        install.run(self)

setup(
    name='psetup',
    version='1.0.1',
    packages=[''],
    url='',
    license='',
    author='tarunlalwani',
    author_email='',
    description='',
    cmdclass={
        'install': InstallLocalPackage
    }
)

A. 如果我没记错的话,可以通过将以下内容添加到 setup.cfg 文件和 setup.py 文件中来实现:

[build_ext]
debug = 1

B.1. 为了更灵活,我相信应该可以在命令行中显式:

$ path/to/pythonX.Y setup.py build_ext --debug install

B.2. 另外如果我理解正确的话应该可以定义所谓的别名

# setup.cfg
[aliases]
release_install = build_ext install
debug_install = build_ext --debug install
$ path/to/pythonX.Y setup.py release_install
$ path/to/pythonX.Y setup.py debug_install

参考资料