我想创建以下模块和对象结构:
main_module
|
`--sub_module
|
+--ObjectOne
|
`--sub_sub_module
|
`--ObjectTwo
当我只有main_module.sub_module
一切正常时,因为我在main_module
文件夹中创建了一个空的__init__.py
(因为main_module
目前没有任何对象),我把sub_module.so
放在它旁边。在
但是,当我试图创建两个独立的c模块来使main_module.sub_module.sub_sub_module
工作(即sub_module.so
和{ImportError
(作为从共享库“引发”的undefined symbol
错误的包装器),因为sub_sub_module
需要sub_module
中的一些C级定义。这让我觉得,创建一个共享库来在其中创建“虚拟”模块,而不是不必要的库链接,这会更容易。。在
所以我的问题是:有可能吗?如果是,怎么办?或者,有没有更好的方法来实现我想要的?在
我想我现在有你的问题了。要想获得详细而详细的答案,您应该阅读Providing a C API for an Extension Module。在
要实现这一点,您应该使用Capsules。对于这些,您希望在另一个模块中使用的每个函数都应该存储在
void*
数组中。然后使用数组作为第一个参数,用PyCapsule_New(void*, const char*, PyCapsule_Destructor)
创建一个胶囊。此对象必须添加到子模块。然后,您可以调用sub_sub_模块中的PyCapsule_Import(const char*, int)
,导入数组并访问所需的函数。在相关问题 更多 >
编程相关推荐