我有一個簡單的C文件擴展python的問題。在C/C++擴展python AttributeError
的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」 。
叫「蟒蛇的setup.py建」後,我可以導入打招呼,但是當我嘗試使用 「hello.say_hello()」我面對的錯誤:
回溯(最近通話最後一個): 文件「<標準輸入>」,1號線,在 AttributeError的:「模塊」對象有「say_hello」
沒有屬性我很感激,如果有人可以幫我找到解決方案。
感謝