how do i use python libraries in C++?
Asked Answered
L

4

12

I want to use the nltk libraries in c++.

Is there a glue language/mechanism I can use to do this?

Reason: I havent done any serious programming in c++ for a while and want to revise NLP concepts at the same time.

Thanks

Labialized answered 17/11, 2008 at 22:3 Comment(0)
L
17

You can also try the Boost.Python library; which has this capability. This library is mainly used to expose C++ to Python, but can be used the other way around.

Lastex answered 17/11, 2008 at 22:18 Comment(2)
Thanks for nice resource. It solved several tiresome hours of me. Is there any concrete example without boost?Transitive
Would it also be possible to import third-party Python libraries (e.g. installed via pip system-wide) from within Boost.Python in C++?Mannes
C
18

Although calling c++ libs from python is more normal - you can call a python module from c++ by bascially calling the python intepreter and have it execute the python source. This is called embedding

Alternatively the boost.python library makes it very easy.

Coleorhiza answered 17/11, 2008 at 22:13 Comment(1)
embedding doc has moved docs.python.org/2.7/extending/embedding.html, I've suggested an edit.Lachus
L
17

You can also try the Boost.Python library; which has this capability. This library is mainly used to expose C++ to Python, but can be used the other way around.

Lastex answered 17/11, 2008 at 22:18 Comment(2)
Thanks for nice resource. It solved several tiresome hours of me. Is there any concrete example without boost?Transitive
Would it also be possible to import third-party Python libraries (e.g. installed via pip system-wide) from within Boost.Python in C++?Mannes
D
3

Pyrex can be cleanly used for this purpose. There's an example in the source-code release.

Diantha answered 18/11, 2008 at 0:58 Comment(0)
C
1

I haven't tried directly calling Python functions from C++, but here are some alternative ideas...

Generally, it's easier to call C++ code from a high-level language like Python than the other way around. If you're interested in this approach, then you could create a C++ codebase and access it from Python. You could either directly use the external API provided by python [it should be described somewhere in the Python docs] or use a tool like SWIG to automate the C++-to-Python wrapping process.

Depending on how you want to use the library, you could alternatively create Python scripts which you call from C++ with the exec* functions.

Centner answered 17/11, 2008 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.