Integrate Python And C++

Question:

I’m learning C++ because it’s a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.

Is it possible to integrate C++ and Python in the same project?

Asked By: Nathan Campos

||

Answers:

You can write Python extensions in C++. Basically Python itself is written in C and you can use that to call into your C code. You have full access to your Python objects. Also check out Boost.Python.

Answered By: Matt Price

Yes, it is possible, encouraged and documented. I have done it myself and found it to be very easy.

Answered By: Otto Allmendinger

Python/C API Reference Manual – the API used by C and C++ programmers who want to write extension modules or embed Python.

Extending and Embedding the Python Interpreter

describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can define new functions but also new object types and their methods. The document also describes how to embed the Python interpreter in another application, for use as an extension language. Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.

Answered By: gimel

Try Pyrex. Makes writing C++ extensions for Python easier.

Answered By: Brian

See this:

Extending Python with C or C++

“It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can’t be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls.

To support extensions, the Python API (Application Programmers Interface) defines a set of functions, macros and variables that provide access to most aspects of the Python run-time system. The Python API is incorporated in a C source file by including the header “Python.h”. “

http://www.python.org/doc/2.5.2/ext/intro.html

PS It’s spelt “integrate” 🙂

Answered By: Larry Watanabe

Interfacing Python with C/C++ is not an easy task.

Here I copy/paste a previous answer on a previous question for the different methods to write a python extension. Featuring Boost.Python, SWIG, Pybindgen…

  • You can write an extension yourself in C or C++ with the Python C-API.

    In a word: don’t do that except for learning how to do it. It’s very difficult to do it correctly. You will have to increment and decrement references by hand and write a lot of code just to expose one function, with very few benefits.

  • Swig:

    pro: you can generate bindings for many scripting languages.

    cons: I don’t like the way the parser works. I don’t know if they’ve made some progress but two years ago the C++ parser was quite limited. Most of the time I had to copy/paste my .h headers to add some % characters and to give extra hints to the swig parser.

    I also needed to deal with the Python C-API from time to time for (not so) complicated type conversions.

    I’m not using it anymore.

  • Boost.Python:

    pro:
    It’s a very complete library. It allows you to do almost everything that is possible with the C-API, but in C++. I never had to write a C-API code with this library. I also never encountered a bug due to the library. Code for bindings either works like a charm or refuses to compile.

    It’s probably one of the best solutions currently available if you already have some C++ library to bind. But if you only have a small C function to rewrite, I would probably try with Cython.

    cons: if you don’t have a precompiled Boost.Python library you’re going to use Bjam (sort of a replacement of make). I really hate Bjam and its syntax.

    Python libraries created with B.P tend to become obese. It also takes a lot of time to compile them.

  • Py++: it’s Boost.Python made easy. Py++ uses a C++ parser to read your code and then generates Boost.Python code automatically. You also have a great support from its author (no it’s not me 😉 ).

    cons: only the problems due to Boost.Python itself.

    Edit this project looks discontinued. While probably still working it may be better to consider switching.

  • Pybindgen:

    It generates the code dealing with the C-API. You can either describe functions and classes in a Python file, or let Pybindgen read your headers and generate bindings automatically (for this it uses pygccxml, a python library wrote by the author of Py++).

    cons: it’s a young project, with a smaller team than Boost.Python. There are still some limitations: you cannot expose your own C++ exceptions, you cannot use multiple inheritance for your C++ classes.

    Anyway it’s worth trying!

  • Pyrex and Cython:

    Here you don’t write real C/C++ but a mix between Python and C. This intermediate code will generate a regular Python module.

Edit Jul 22 2013: Now Py++ looks discontinued, I’m now looking for a good alternative. I’m currently experimenting with Cython for my C++ library. This language is a mix between Python and C. Within a Cython function you can use either Python or C/C++ entities (functions, variables, objects, …).

Cython is quite easy to learn, has very good performance, and you can even avoid C/C++ completely if you don’t have to interface legacy C++ libraries.

However for C++ it comes with some problems. It is less "automagic" than Py++ was, so it’s probably better for stable C++ API (which is now the case of my library). The biggest problem I see with Cython is with C++ polymorphism. With Py++/boost:python I was able to define a virtual method in C++, override it in Python, and have the Python version called within C++. With Cython it’s still doable but you need to explicitly use the C-Python API.

Edit 2017-10-06:

There is a new one, pybind11, similar to Boost.Python but with some potential advantages. For example it uses C++11 language features to make it simpler to create new bindings. Also it is a header-only library, so there is nothing to compile before using it, and no library to link.

I played with it a little bit and it was indeed quite simple and pleasant to use. My only fear is that like Boot.Python it could lead to long compilation time and large libraries. I haven’t done any benchmark yet.

Answered By: ascobol

I’ve used PyCxx http://cxx.sourceforge.net/ in the past and i found that it was very good.

It wraps the python c API in a very elegant manner and makes it very simple to use.
It is very easy to write python extension in c++. It is provided with clear examples so it is easy to get started.

I’ve really enjoyed using this library and I do recommend it.

Answered By: luc

We use swig very successfully in our product.

Basically swig takes your C++ code and generates a python wrapper around it.

Answered By: Alan

It depends on your portability requirements. I’ve been struggling with this for a while, and I ended up wrapping my C++ using the python API directly because I need something that works on systems where the admin has only hacked together a mostly-working gcc and python installation.

In theory Boost.Python should be a very good option, since Boost is available (almost) everywhere. Unfortunately, if you end up on a OS with an older default python installation (our collaboration is stuck with 2.4), you’ll run into problems if you try to run Boost.Python with a newer version (we all use Python 2.6). Since your admin likely didn’t bother to install a version of Boost corresponding to every python version, you’ll have to build it yourself.

So if you don’t mind possibly requiring some Boost setup on every system your code runs on, use Boost.Python. If you want code that will definitely work on any system with Python and a C++ compiler, use the Python API.

Answered By: Shep

Another interesting way to do is python code generation by running python itself to parse c++ header files. OpenCV team successfully took this approach and now they have done exact same thing to make java wrapper for OpenCV library. I found this created cleaner Python API without limitation caused by a certain library.

Answered By: Tae-Sung Shin

I’d recommend looking at how PyTorch does their integration.

Answered By: Andrei Pokrovsky

try using pybind11

Also, this is a really good tutorial

Answered By: Kellan_B
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.