如何在pycharm中使用pybind11创建的C++模块

问题描述

我正在寻找一种在我的 Python 项目中使用 C++ 代码方法。 为此,我使用 pybind11,并使用以下 youtube 视频作为教程: https://www.youtube.com/watch?v=-eIkUnCLMFc&list=PLb9uFnQyeGTcKIHNUNUUuLbRhumAZd-fy&index=1

根据该教程,我成功创建了一个基本的 C++ 模块,并设法在 cmd 中使用 python 命令提示符运行代码

这是 C++ 代码

#include <pybind11/pybind11.h>
#include <stdio.h>

void say_hello()
{
    printf("Hello World from C++\n");
}

PYBIND11_MODULE(pybind11module,module)
{
    module.doc() = "Pybind11Module";
    module.def("say_hello",&say_hello);
}

这是在python命令提示符下成功运行:

enter image description here

我想知道有没有办法使用我在其他地方创建的模块?例如pycharm?

感谢您的帮助

解决方法

感谢 Justin 的评论,我在互联网上搜索了如何设置 PYTHONPATHsys.path 的值。

终于找到了下一个链接: https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-reloading-interpreter-paths.html

此链接涵盖的主题是:如何安装、卸载和升级解释器路径。 所以,为了在 python 项目中使用我的 c++ 代码,我所要做的就是输入 cpp 模块的路径,这解决了我的问题。