[英]What is the easiest way to make an optional C extension for a python package?
I've created a C extension that I'd like to enable in my Python package (using setuptools) only if a command line option is passed in. What is the easiest way to do this? 我已经创建了一个C扩展,我想在我的Python包中启用(使用setuptools),只有传入命令行选项。最简单的方法是什么?
I can't seem to find any straightforward ways of going about this. 我似乎无法找到任何直截了当的方式来解决这个问题。
There's actually a distribute/setuptools feature called "Features" that can be used for this. 实际上有一个名为“Features”的分发/设置工具功能可以用于此目的。 It's explicitly designed to have setup.py do different things based on
--with-xxx
and --without-xxx
command line options. 它明确地设计为让setup.py根据
--with-xxx
和--without-xxx
命令行选项执行不同的操作。
ext_modules = []
if '--add-this' in sys.argv:
ext_modules.append(Extension(...))
sys.argv.remove('--add-this')
setup(...
ext_modules = ext_modules
)
This is hacky, but might be easiest. 这很hacky,但可能最容易。 A more advanced approach would be to extend the Distribution class to support a flag, say
--with-modules
and then customize ext_modules inside finalize_options. 更高级的方法是扩展Distribution类以支持标志,比如
--with-modules
,然后在finalize_options中自定义ext_modules。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.