在C++中增强Python NoMPy

2024-09-30 10:29:43 发布

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

我试图使用引导Python库将NUMPY数组传递给C++。我按照Python 3.7 + Visual Studio 2107 + boost 1.69Using boost numpy with visual studio 2019 and python 3.8中描述的说明创建boost_numpy37-vc142-mt-gd-x64-1_75.lib和libboost_numpy37-vc142-mt-gd-x64-1_75.lib。visual studio dll项目符合要求,没有任何问题。但是,当我尝试在python中导入.pyd时,总是会收到错误消息“ImportError:DLL加载失败:找不到指定的模块”

下面是我的示例代码(从https://www.boost.org/doc/libs/1_75_0/libs/python/doc/html/numpy/tutorial/simple.html获得)

#define BOOST_PYTHON_STATIC_LIB
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include "pch.h"
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
using namespace boost::python;
namespace p = boost::python;
namespace np = boost::python::numpy;

class Example {
public:
    Example() {}   
void ArrayFromPyArray()
    {
        p::tuple shape = p::make_tuple(3, 3);
        np::dtype dtype = np::dtype::get_builtin<float>();
        np::ndarray a = np::zeros(shape, dtype);
    }
};

BOOST_PYTHON_MODULE(DRTCpp)
{
    Py_Initialize();
    numpy::initialize();
    class_<Example>("Example")
       .def("ArrayFromPyArray", &Example::ArrayFromPyArray)
        ;
}

python代码是

import numpy as np
from DRTCpp import Example
e=Example()
e.ArrayFromPyArray()

当我运行这段代码时,我得到了消息

Traceback (most recent call last):
  File "C:\Users\manus\Desktop\learning python\vs\DRT\DRT\DRT.py", line 4, in <module>
    from DRTCpp import Example
ImportError: DLL load failed: The specified module could not be found.


Tags: 代码importnumpyincludeexamplenpnamespacestudio

热门问题