Python:AttributeError:'module'对象没有属性

时间:2022-06-29 08:16:49

I used swig to create a python file from c. I have converted the c file into .py file and when I try to invoke a function of the c program, I am getting an error AttributeError: 'module' object has no attribute 'fact'

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

My C file is

我的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);
 }

My interface file is

我的界面文件是

   /* 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();
 %}

can somebody help me?

有人能帮助我吗?

1 个解决方案

#1


You have to inline your declarations in example.i:

你必须在example.i中内联你的声明:

%module example
%inline %{

From [SWIG]: Inlined code blocks:

来自[SWIG]:内联代码块:

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

%inline指令将所有跟随文字的代码插入到接口文件的标题部分中。

#1


You have to inline your declarations in example.i:

你必须在example.i中内联你的声明:

%module example
%inline %{

From [SWIG]: Inlined code blocks:

来自[SWIG]:内联代码块:

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

%inline指令将所有跟随文字的代码插入到接口文件的标题部分中。