简体   繁体   English

未找到来自导入的 swig 包装模块的功能

[英]Function from imported swig wrapped module not found

I want to create several Python modules from SWIG-wrapped C++ classes.我想从 SWIG 包装的 C++ 类创建几个 Python 模块。 The problem is that modules cannot import each other.问题是模块不能相互导入。

here is A_module.i file这是 A_module.i 文件

%module A_module
%inline
%{
    void foo(){}
%}

it compiles without problem, thus in .py script I can do something like this:它编译没有问题,因此在 .py 脚本中我可以做这样的事情:

import A_module
A_module.foo()

Now I want to create B_module that import A_module and calls foo.现在我想创建导入 A_module 并调用 foo 的 B_module。 Here is B_module.i file这是 B_module.i 文件

%module B_module
%import A_module.i

When I do something like this in .py当我在 .py 中做这样的事情时

import B_module
B_module.foo()

It gives an error syaing module has no attribute foo.它给出了一个错误 syaing 模块没有属性 foo。

The solution I've found is to use %include instead of import, but it creates same foo function in both modules, which I would like to avoid我发现的解决方案是使用 %include 而不是 import,但它在两个模块中创建了相同的 foo 函数,我想避免这种情况

That .i %import is a Python import , so the foo you're looking for lives as B_module.A_module.foo .那个 .i %import是一个 Python import ,所以你正在寻找的fooB_module.A_module.foo You can use Python code explicitly to import and copy parts of A_module into B_module.您可以显式地使用 Python 代码将 A_module 的部分导入和复制到 B_module 中。 For example, have as B_module.i :例如,有B_module.i

%module B_module

%pythoncode %{
import A_module
foo = A_module.foo
del A_module
%}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM