简体   繁体   中英

Python: AttributeError: 'module' object has no attribute

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'

My C file is

/* 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?

You have to inline your declarations in example.i :

%module example
%inline %{

From [SWIG]: Inlined code blocks :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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