Python: AttributeError: 'module' object has no attribute

Python: AttributeError: 'module' object has no attribute

我使用 swig 从 c 创建了一个 python 文件。我已将 c 文件转换为 .py 文件,当我尝试调用 c 程序的函数时,出现错误 AttributeError: 'module' 对象没有属性 'fact'

我的 C 文件是

/* File : example.c */


 #include <time.h>
 double My_variable = 3.0;

 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }

 int my_mod(int x, int y) {
     return (x%y);
 }

 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }

我的接口文件是

   /* example.i */
 %module example
 %{
 /* Put header files here or function declarations like below */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}

有人可以帮我吗?

您必须 内联 您在 example.i:

中的声明
%module example
%inline %{

来自[SWIG]: Inlined code blocks

The %inline directive inserts all of the code that follows verbatim into the header portion of an interface file.