gpt4 book ai didi

python - 从 C 代码构建 Python 模块失败

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:26 24 4
gpt4 key购买 nike

我正在阅读 this tutorial关于如何绑定(bind) Python 和 C。我使用的是 Python v3.7.1,所以我必须使用新的 Python 界面(与示例不同),所以现在我有:

加法器.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 running install running build running build_ext running install_lib running install_egg_info Traceback (most recent call last): File "setup.py", line 4, in ext_modules=[Extension('addList', ['adder.c'])]) File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 966, in run_commands self.run_command(cmd) File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 985, in run_command cmd_obj.run() File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install.py", line 557, in run self.run_command(cmd_name) File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 984, in run_command cmd_obj.ensure_finalized() File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\cmd.py", line 107, in ensure_finalized self.finalize_options() File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install_egg_info.py", line 26, in finalize_options to_filename(safe_version(self.distribution.get_version())), File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install_egg_info.py", line 68, in safe_version version = version.replace(' ','.') AttributeError: 'float' object has no attribute '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))

最佳答案

错误信息是这样的:

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

你的构建脚本说:

version=1.0

显然它需要一个字符串,而不是一个 float 。

关于python - 从 C 代码构建 Python 模块失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53247742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com