如何使用 python setuptools 查看已选择的附加项
我有一个包含本机 C++ 扩展的 python 包。本机扩展有一些编译选项,可以使用宏打开/关闭。其中之一就是包括开罗支持。所以这也有 python 对 pycairo 的依赖。但这应该是可选的。因此,我为 cairo
指定了一个 extras_require
参数,将 pycairo
列为依赖项。现在,我如何查看用户在构建/安装期间在运行时实际指定了哪些额外内容,因此我还可以相应地修改 extra_compile_args 以添加 -DWITH_PYCAIRO
。不幸的是 api 文档 甚至没有列出setup()
的 extras_require
参数。
这是我迄今为止所掌握的内容的缩写版本。
setup(
name="mypackage",
packages="mypackage",
extras_require={
"cairo": ["pycairo"]
},
ext_modules=[Extension(
"mypackage._mypackage",
[ "src/my_package.cpp"],
language="c++",
extra_compile_args=["-DOPTION1"]
)],
)
我看到实现这一点的一种方法是使用一个额外的环境变量,如下所示:
extra_compile_args=["-DOPTION1"]
if os.environ.get('WITH_CAIRO', False):
extra_compile_args.append("-DWITH_PYCAIRO")
setup(
.....
ext_modules=[Extension(
"mypackage._mypackage",
[ "src/my_package.cpp"],
language="c++",
extra_compile_args=extra_compile_args
)],
)
然后使用 WITH_CAIRO pip install .[pycairo]
安装,但这似乎是多余的,两次指定相同的选项。
I have a python package that includes a native c++ extension. The native extension has a few options for compilation that can be turned on/off with a macro. One of those is including cairo support. So that also has the python dependency on pycairo. But this should be optional. So I specified an extras_require
argument for cairo
that lists pycairo
as dependency. Now how do I see what extras have actually been specified by the user at run time during build/installation, so I can also modify the extra_compile_args accordingly to add -DWITH_PYCAIRO
. Unfortunately the api docs don't even list the extras_require
argument to setup()
.
This is an abbreviated version of what I have so far.
setup(
name="mypackage",
packages="mypackage",
extras_require={
"cairo": ["pycairo"]
},
ext_modules=[Extension(
"mypackage._mypackage",
[ "src/my_package.cpp"],
language="c++",
extra_compile_args=["-DOPTION1"]
)],
)
One way I can see of implementing this is to use an additional environment variable like this:
extra_compile_args=["-DOPTION1"]
if os.environ.get('WITH_CAIRO', False):
extra_compile_args.append("-DWITH_PYCAIRO")
setup(
.....
ext_modules=[Extension(
"mypackage._mypackage",
[ "src/my_package.cpp"],
language="c++",
extra_compile_args=extra_compile_args
)],
)
and then install with WITH_CAIRO pip install .[pycairo]
but that seems kind of superfluous to specify the same option twice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论