【问题标题】:Building a Python module from C code fails从 C 代码构建 Python 模块失败
【发布时间】:2019-04-14 07:44:52
【问题描述】:

我正在阅读this tutorial,了解如何绑定 Python 和 C。 我使用的是 Python v3.7.1,所以我不得不使用新的 Python 接口(与示例不同),所以现在我有了:

adder.c

#include <Python.h>

static PyObject *addList_add(PyObject *self, PyObject *args)
{
    PyObject *listObj;

    if (!PyArg_ParseTuple(args, "O", &listObj))
        return NULL;

    Py_ssize_t length = PyList_Size(listObj);
    long i, sum = 0;
    for (i = 0; i < length; i++)
    {
        PyObject *temp = PyList_GetItem(listObj, i);
        long elem = PyLong_AsLong(temp);
        sum += elem;
    }

    return Py_BuildValue("i", sum);
}

static char addList_docs[] = "add(): add all elements of the list\n";

static PyMethodDef addLust_funcs[] = {
    {"add", (PyCFunction)addList_add, METH_VARARGS, addList_docs},
    {NULL, NULL, 0, NULL}};

static struct PyModuleDef addList = {
    PyModuleDef_HEAD_INIT,
    "addList",
    addList_docs,
    -1,
    addLust_funcs};

PyMODINIT_FUNC PyInit_addList(void)
{
    return PyModule_Create(&addList);
}

似乎一切都正确,我可以通过调用 addList.add() 函数来运行 Python 代码。但是在构建模块时,我得到了这个输出(请注意,在这个错误之后我可以很好地运行 Python 代码):

$ python setup.py install 运行安装 运行构建 运行 build_ext 运行 install_lib 运行 install_egg_info 回溯(最近一次通话最后): 文件“setup.py”,第 4 行,在 ext_modules=[扩展('addList', ['adder.c'])]) 文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\core.py”,第 148 行,在设置中 dist.run_commands() 文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py”,第 966 行,在 run_commands self.run_command(cmd) 文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py”,第 985 行,在 run_command cmd_obj.run() 运行中的文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install.py”,第 557 行 self.run_command(cmd_name) 文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\cmd.py”,第 313 行,在 run_command self.distribution.run_command(command) 文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py”,第 984 行,在 run_command cmd_obj.ensure_finalized() 文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\cmd.py”,第 107 行,在 ensure_finalized self.finalize_options() 文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install_egg_info.py”,第 26 行,在 finalize_options to_filename(safe_version(self.distribution.get_version())), 文件“C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install_egg_info.py”,第 68 行,在 safe_version 版本 = version.replace('','.') AttributeError: 'float' 对象没有属性 'replace'

setup.py

from distutils.core import setup, Extension

setup(name='addList', version=1.0,
      ext_modules=[Extension('addList', ['adder.c'])])

main.py

import addList

l = [1, 2, 3, 5, 4]

print(addList.add(l))

【问题讨论】:

    标签: python c python-3.x api language-interoperability


    【解决方案1】:

    错误信息说:

    version.replace('','.') AttributeError: 'float' object has no attribute 'replace'

    你的构建脚本说:

    version=1.0
    

    显然它需要一个字符串,而不是浮点数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2021-04-27
      • 2013-02-01
      • 2019-12-26
      • 2019-11-06
      • 2017-10-29
      • 2015-12-23
      相关资源
      最近更新 更多