尝试在Python3.6中安装cgpyencode时返回没有值的语句

2024-10-01 22:36:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我的公司正在从Python2.7迁移到Python3.6,我正在尝试安装cgpyencode。但这根本不起作用,我得到了错误

 gpolyencode_py.cpp:187:69: error: ‘Py_InitModule3’ was not declared in this scope
                            "Google Maps Polyline encoding (C extension)");
                                                                         ^
    gpolyencode_py.cpp:190:9: error: return-statement with no value, in function returning ‘PyObject* {aka _object*}’ [-fpermissive]
             return;
             ^
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

每次。一些快速的谷歌搜索似乎表明模块本身需要重写,而我没有资格这样做。我已经尝试过安装python3.6-devlibxml2-dev等基础知识,但问题仍然存在。有没有一种解决方法或方法可以在不重建的情况下安装它?在


Tags: 方法inpyreturn错误withnot公司
1条回答
网友
1楼 · 发布于 2024-10-01 22:36:49

python2和python3之间的C扩展是非常不同的。但是,您可以使用预处理器编写一个同时满足这两个条件的文件。下面是一个非常基本的C模块from my github

#include <Python.h>

static PyObject* _hello_world(PyObject* self) {
    return PyUnicode_FromString("hello world");
}

static struct PyMethodDef methods[] = {
    {"hello_world", (PyCFunction)_hello_world, METH_NOARGS},
    {NULL, NULL}
};

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef module = {
    PyModuleDef_HEAD_INIT,
    "basic_c_module",
    NULL,
    -1,
    methods
};

PyMODINIT_FUNC PyInit_basic_c_module(void) {
    return PyModule_Create(&module);
}
#else
PyMODINIT_FUNC initbasic_c_module(void) {
    Py_InitModule3("basic_c_module", methods, NULL);
}
#endif

注意,#else分支可能与当前模块的类似,您需要更新它,使其看起来更像#if PY_MAJOR_VERSION >= 3分支

相关问题 更多 >

    热门问题