Reputation: 87
I'm looking for a way to use C++ code inside my python project. For that, I'm using pybind11, and i used the following youtube videos as a tutorial: https://www.youtube.com/watch?v=-eIkUnCLMFc&list=PLb9uFnQyeGTcKIHNUNUUuLbRhumAZd-fy&index=1
Based on that tutorial, I've successfully created a basic C++ module and managed to run the code using python command prompt in the cmd.
Here's the C++ code:
#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);
}
And here's the successful run in the python command prompt:
I was wondering is there a way to use the module i've created in other places as well? for example pycharm?
Thank you for your help
Upvotes: 2
Views: 1761
Reputation: 87
Thanks to Justin's comment, I've searched the internet for how to set a value for PYTHONPATH
or sys.path
.
Finally I found the next link: https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-reloading-interpreter-paths.html
This link covers the subject: how to install, uninstall, and upgrade interpreter paths. So, all i had to do in order to use my c++ code in python project was enter the path of the cpp module, and that solved my problem.
Upvotes: 2