【发布时间】:2013-08-27 11:22:49
【问题描述】:
我在使用简单的 C 文件扩展 python 时遇到问题。
hello.c 源代码:
#include <Python.h>
static PyObject* say_hello(PyObject* self, PyObject* args)
{
const char* name;
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;
printf("Hello %s!\n", name);
Py_RETURN_NONE;
}
static PyMethodDef HelloMethods[] =
{
{"say_hello", say_hello, METH_VARARGS, "Greet somebody."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
inithello(void)
{
(void) Py_InitModule("hello", HelloMethods);
}
setup.py:
from distutils.core import setup, Extension
module1 = Extension('hello', sources = ['hello.c'])
setup (name = 'PackageName',
version = '1.0',
packages=['hello'],
description = 'This is a demo package',
ext_modules = [module1])
我还在文件夹“hello”中创建了空文件“__init__.py”。
调用“python setup.py build”后,我可以导入你好,但是当我尝试 使用“hello.say_hello()”我会遇到错误:
Traceback(最近一次调用最后一次):
文件“
如果有人可以帮助我找到解决方案,我将不胜感激。
谢谢
【问题讨论】:
标签: c++ python swig distutils attributeerror