pythonswig

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?


Solution

  • 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.