gpt4 book ai didi

python - PyArrayObject 没有成员

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:08 34 4
gpt4 key购买 nike

我正在尝试为我的 python/numpy 代码编写一个简单的 C++ 扩展。但是,我无法编译扩展脚本,因为函数输入中的 PyArrayObject 没有成员。在我看来,我正在做与例如 this post 相同的事情。 ,但我想我错过了一些东西。这是一个编译失败的示例,因为我尝试检索维度成员:

#include <Python.h>
#include <stdio.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy/arrayobject.h"

static PyObject *function(PyObject *self, PyObject *args) {

PyObject *input;
PyArrayObject *array;

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

array= (PyArrayObject *)
PyArray_ContiguousFromObject(input, NPY_DOUBLE, 2, 2);
long n=array->dimensions[1];
}

这是在 linux 系统和 windows 7 计算机上编译的,使用 MVS 14.0 c++ 编译器,因此问题似乎与平台无关。

Python 版本:3.5

windows系统异常输出:

paneltime/cfunctions.cpp(20): error C2039: 'dimensions': is not a member of 'tagPyArrayObject'c:\anaconda3\lib\site-packages\numpy\core\include\numpy\ndarraytypes.h(692): note: see declaration of 'tagPyArrayObject'
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2

最佳答案

我在关注已发布的示例时也遇到了这个问题。我相信不推荐使用维度成员。相反,应该使用 PyArray_DIM 或 PyArray_DIMS(参见 NumPy docs)

例如:

long n=PyArray_DIM(array,1);

关于python - PyArrayObject 没有成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590073/

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