I have a problem with extending python with a simple C file.
hello.c source code:
#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])
I also created empty file "__init__.py" in the folder "hello".
After calling "python setup.py build" I could import hello, but when I try to use "hello.say_hello()" I face with the error:
Traceback (most recent call last): File "< stdin>", line 1, in AttributeError: 'module' object has no attribute 'say_hello'
I appreciate if somebody can help me to find the solution.
Thanks