如何为 python 制作简单的 Hello World c 模块?
How to make simple Hello World c module for python?
在过去的几个小时里,我一直在尝试构建 运行 一个打印“hello world!!”的简单 c 模块。我 运行 遇到了一个我似乎无法解决的问题:
正在导入模块:
import hi
Traceback (most recent call last):
File "<ipython-input-1-746bfab23d87>", line 1, in <module>
import hi
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 2: invalid start byte
hellomodule.c:
#include <Python.h>
static PyObject *hello(void);
static PyMethodDef module_methods[] = {
{"hello", hello, METH_VARARGS},
{NULL, NULL, 0}
};
PyMODINIT_FUNC PyInit_hi(void)
{
PyObject *module;
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"hi",
module_methods,
NULL,
NULL,
NULL,
};
module = PyModule_Create(&moduledef);
return module;
}
static PyObject *hello(void)
{
printf("%s", "hello world");
return Py_BuildValue("Pls Work");
}
setup.py:
from distutils.core import setup, Extension
module1 = Extension('hi', sources=['hellomodule.c'])
setup(name='MyExtension',
version='1.0',
description='This is a demo extension',
ext_modules=[module1])
我正在导入的实际模块显然是从“setup.py 构建”创建的 .pyc。此代码适用于 windows 环境。任何帮助表示赞赏!谢谢!!
主要问题在于如何初始化 PyModuleDef
def 结构。从 the documentation 可以看出,您需要将 module_methods
作为第 5 个参数而不是第 3 个参数传入,以及其他关键参数:
PyMODINIT_FUNC PyInit_hi(void)
{
PyObject *module;
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"hi",
NULL,
-1,
module_methods,
};
module = PyModule_Create(&moduledef);
return module;
}
此外,在 hello
函数本身中,如果您想立即刷新打印语句,您可能需要一个换行符,并且 Py_BuildValue
需要将适当的格式字符传递给 return 一个字符串:
static PyObject *hello(void)
{
printf("%s", "hello world\n");
return Py_BuildValue("s", "Pls Work");
}
在过去的几个小时里,我一直在尝试构建 运行 一个打印“hello world!!”的简单 c 模块。我 运行 遇到了一个我似乎无法解决的问题:
正在导入模块:
import hi
Traceback (most recent call last):
File "<ipython-input-1-746bfab23d87>", line 1, in <module>
import hi
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 2: invalid start byte
hellomodule.c:
#include <Python.h>
static PyObject *hello(void);
static PyMethodDef module_methods[] = {
{"hello", hello, METH_VARARGS},
{NULL, NULL, 0}
};
PyMODINIT_FUNC PyInit_hi(void)
{
PyObject *module;
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"hi",
module_methods,
NULL,
NULL,
NULL,
};
module = PyModule_Create(&moduledef);
return module;
}
static PyObject *hello(void)
{
printf("%s", "hello world");
return Py_BuildValue("Pls Work");
}
setup.py:
from distutils.core import setup, Extension
module1 = Extension('hi', sources=['hellomodule.c'])
setup(name='MyExtension',
version='1.0',
description='This is a demo extension',
ext_modules=[module1])
我正在导入的实际模块显然是从“setup.py 构建”创建的 .pyc。此代码适用于 windows 环境。任何帮助表示赞赏!谢谢!!
主要问题在于如何初始化 PyModuleDef
def 结构。从 the documentation 可以看出,您需要将 module_methods
作为第 5 个参数而不是第 3 个参数传入,以及其他关键参数:
PyMODINIT_FUNC PyInit_hi(void)
{
PyObject *module;
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"hi",
NULL,
-1,
module_methods,
};
module = PyModule_Create(&moduledef);
return module;
}
此外,在 hello
函数本身中,如果您想立即刷新打印语句,您可能需要一个换行符,并且 Py_BuildValue
需要将适当的格式字符传递给 return 一个字符串:
static PyObject *hello(void)
{
printf("%s", "hello world\n");
return Py_BuildValue("s", "Pls Work");
}