Python:AttributeError:“模块”对象没有属性

问题描述 投票:0回答:1

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

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

有人可以帮助我吗?

python swig
1个回答
3
投票

您必须在example.i内联声明:

%module example
%inline %{

来自 [SWIG]:内联代码块

%inline 指令将逐字后面的所有代码插入到接口文件的标头部分。

© www.soinside.com 2019 - 2024. All rights reserved.