我想在一个包下收集多个Python模块,因此它们不会从全局的python包和模块集中保留太多名称.但我用C编写的模块有问题.
这是一个非常简单的例子,直接来自官方Python文档.您可以在此处的页面底部找到它:http://docs.python.org/distutils/examples.html
from distutils.core import setup
from distutils.extension import Extension
setup(name='foobar',
version='1.0',
ext_modules=[Extension('foopkg.foo', ['foo.c'])],
)
Run Code Online (Sandbox Code Playgroud)
我的foo.c文件看起来像这样
#include <Python.h>
static PyObject *
foo_bar(PyObject *self, PyObject *args);
static PyMethodDef FooMethods[] = {
{
"bar",
foo_bar,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};
static PyObject *
foo_bar(PyObject *self, PyObject *args)
{
return Py_BuildValue("s", "foobar");
}
PyMODINIT_FUNC
initfoo(void)
{
(void)Py_InitModule("foo", FooMethods);
}
int
main(int argc, char *argv[])
{
// Pass argv[0] to the Python interpreter
Py_SetProgramName(argv[0]);
// Initialize the Python …Run Code Online (Sandbox Code Playgroud)