python – SWIG:AttributeError:’module’对象没有属性’fact
发布时间:2020-12-20 13:49:27 所属栏目:Python 来源:网络整理
导读:我正在研究如何使用swig为我的 python代码进行C扩展.我使用我从网站获得的代码作为示例. 这是我的代码: 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)
我正在研究如何使用swig为我的
python代码进行C扩展.我使用我从网站获得的代码作为示例.
这是我的代码: 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); } example.h文件 #ifndef EXAMPLE_H_ #define EXAMPLE_H_ extern double My_variable; extern int fact(int n); extern int my_mod(int x,int y); #endif example.i %module example %{ /* Put header files here or function declarations like below */ #define SWIG_FILE_WITH_INIT #include "example.h" %} %#include "example.h" Makefile文件 all: rm -f *.so *.o *_wrap.* *.pyc swig -python example.i gcc -c -fPIC example_wrap.c -I/usr/include/python2.7 gcc -shared example_wrap.o -o _example.so clean: rm -f *.so *.o *_wrap.* *.pyc test.py import example print str(example.fact(2)) test.py用于检查扩展是否有效.但是当我运行test.py时,它输出: Traceback (most recent call last): File "test.py",line 3,in <module> print str(example.fact(2)) AttributeError: 'module' object has no attribute 'fact' 这是我使用dir时的输出(例子): ['__builtins__','__doc__','__file__','__name__','__package__','_example','_newclass','_object','_swig_getattr','_swig_property','_swig_repr','_swig_setattr','_swig_setattr_nondynamic'] 出现这个输出的原因是什么? 如果我想要成功运行该程序,我该怎么办? 解决方法
请尝试更换如下:
%#include "example.h" 通过 %include "example.h" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |