Python 3.8:运行外部C++代码:无法导入模块

Python 3.8: Running External C++ Code: Unable to Import Module

本文关键字:导入 模块 代码 外部 运行 Python C++      更新时间:2023-10-16

我正在构建一个C++DLL文件,我正在尝试将其加载到Python模块中。
我正在使用官方的Python文档。C++代码(包括所有样板(如下(酒窝"hello"函数(:

#include <Python.h>
#include <iostream>
PyObject* fft_hello(PyObject *self, PyObject *args){
std::cout << "Insid fft hello" << std::endl;
return nullptr;
}
static PyMethodDef fft_methods[2] = {
{"hello", fft_hello, METH_VARARGS, "Runs hello"},
{nullptr, nullptr, 0, nullptr}
};
static struct PyModuleDef fft_module = {
PyModuleDef_HEAD_INIT,
"fft",
"fft module",
0,
fft_methods
};
PyMODINIT_FUNC
PyInit_fft(void){
std::cout << "INITING" << std::endl;
return PyModule_Create(&fft_module);
}

编译模块的 CMAKE 是:

cmake_minimum_required(VERSION 3.16)
project(fft)
set(CMAKE_CXX_STANDARD 14)
link_directories("C:/Users/guyy/AppData/Local/Programs/Python/Python38/libs")
add_library(fft SHARED fft.cpp )
include_directories("C:/Users/guyy/AppData/Local/Programs/Python/Python38/include")

这将生成一个fft.dll模块。

接下来,我将这个 dll 放在 python 文件目录中并尝试导入它:

import fft
if __name__ == '__main__':
pass

我收到ModuleNotFoundError: No module named 'fft'错误。

那么,我做错了什么?

  • C++文件写错了吗?
  • DLL 模块是否构建错误?
  • DLL 模块是否放在错误的位置?

检查:https://docs.python.org/2/library/ctypes.html 或CPython。

[已编辑] 您也可以使用使用distutils。在根文件夹中创建一个名为setup.py的文件,该文件具有以下实现:

从 distutils.core 导入设置,扩展

def main():
setup(name="fft",
version="1.0.0",
description="Python interface for the fft C library function",
author="<your name>",
author_email="your_email@gmail.com",
ext_modules=[Extension("fft", ["fft.cpp"])])
if __name__ == "__main__":
main()

运行以下命令:

python setup.py install

基本上,这将创建一个名为 fft%python_version_and_distribution%.pyc 的模块,您可以将其添加到文件夹的根目录中。这个模块现在可以通过你的python文件读取。 您也可以在此模块上运行 cmake,但它稍微复杂一些。 我建议检查 distutils 库以获取更多详细信息。

我希望这有帮助

您是否绝对要求将 DLL 作为"模块"加载?

使用 CTYPES 并不复杂。我将它与Python 3.8和VC++ 2010生成的DLL一起使用(此DLL实现了多线程代码并且运行良好(。

在 C/C++ 代码端: -为所有可调用函数的前缀"extern "C",最好对变量和指针使用标准类型,例如:

extern "C" int myTestFunc(int par1, int par2, float *array1)
{
...
return 0;
}

在 Python 代码端: -导入以下模块: 导入 ctypes 导入_ctypes

-在您的代码/类中,将 DLL 加载为:

self.lib = ctypes.CDLL('<full_path_to_your_DLL>')

-声明函数的返回类型(函数名称必须与 C 代码中的相同(:

self.lib.myTestFunc.restype = ctypes.c_int

-定义函数参数类型(下面是标准值参数以及指向 numpy 浮点数组的指针(:

self.lib.myTestFunc.argtypes = [ctypes.c_int, ctypes.c_int, np.ctypeslib.ndpointer(dtype=np.float32)]

-在您的代码中,只需调用该函数:

val1 = 1
val2 = 2
float_array = np.zeros((20, 50),dtype=np.float32)
retval = self.lib.myTestFunc(val1,val2,float_array)

-如果需要释放 DLL 访问权限,可以使用以下内容:

_ctypes.dlclose(self.lib._handle)