Is it possibile to use python libraries in C++ like selenium , django etc ....? If yes are there any docs that explain this well like fully embedding a python library in C++ not just some run script like PyRun_SimpleString() ???
-
What do you want to do with the libraries? – Alan Birtles Mar 19 '22 at 17:07
-
3Use them in C++? seamlessly like in python – S.T.A.L.K.E.R Mar 19 '22 at 17:07
-
1I'm still not sure what you're trying to do but if you want to use python libraries it would seem that your best option is to use python rather than c++ – Alan Birtles Mar 19 '22 at 17:11
-
Many Python libraries are already written in C (or C++) and can be linked to your program. Remember that Python requires an interpreter to run. – Thomas Matthews Mar 19 '22 at 20:49
2 Answers
You can use Python.h
, which is the C API for python. A brief description on how to do this is given here: https://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code.
The python C API's documentation can be found here: https://docs.python.org/3/c-api/index.html.
A suggestion, however: Whatever you're trying to do probably already has existing C++ libraries to do it. I'd advise against using a python library unless absolutely necessary. Or you could just stick to pure python.

- 1,355
- 1
- 7
- 11
If you want to mix Python in a C++ project, a simple possibility is to include Boost in your project, and rely on the Boost.Python library which enables interoperability between Pyton and C++ (https://www.boost.org/doc/libs/1_78_0/libs/python/doc/html/index.html); This is basically a wrapper around the Python C API to make things easier, included into the well-known C++ Boost library.
In your case, you want to embed Python in C++, and Boost almost enables to do it without calling the Python C/ API: https://www.boost.org/doc/libs/1_78_0/libs/python/doc/html/tutorial/tutorial/embedding.html.
The use is pretty straightforward, you simply need to include boost/python.hpp
, and embed the Python interpreter into your code snippet to call Python modules.
Without reinventing the wheel, here's a link to another SO question related to your topic with a nice answer which explains how to do it: How to import a function from python file by Boost.Python.
Of course, you may not want to include Boost into your project (if you don't want to struggle installing it for instance).

- 416
- 3
- 13