Using `pybind11` to Interface Python with C++: Part 1 - Installation

Posted by hnqiu on May 19, 2020

Introduction

pybind11 is an open-source Python binding library that allows you to operate modern C++11 in Python. It can wrap the C++ data types and methods as a callable Python module that creates seamless operability between C++ and Python.

Generally you will need Python bindings either because you want to use some C++ libraries in your Python programs, or because you want to create some Python test cases for your C++ projects. I first came to pybind11 because I wanted to test my neuroevolution algorithm on a classic Flappy Bird game.

In this series of posts I will provide a simple guide on how to create Python bindings for a C++ program. We will begin with the installation of pybind11 in this post as an appetizer.

Installing pybind11 Using conda

We install pybind11 in the conda environment:

conda install -c conda-forge pybind11

Note pybind11 can be installed via pip:

pip3 install pybind11

However, I would not recommend this because pip will only install the necessary C++ headers but not the other files. When I was using cmake to include the pybind11 library externally, it gave the following error:

...

CMake Error at CMakeLists.txt:8 (find_package):
  By not providing "Findpybind11.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "pybind11",
  but CMake did not find one.

  Could not find a package configuration file provided by "pybind11" with any
  of the following names:

    pybind11Config.cmake
    pybind11-config.cmake

  Add the installation prefix of "pybind11" to CMAKE_PREFIX_PATH or set
  "pybind11_DIR" to a directory containing one of the above files.  If
  "pybind11" provides a separate development package or SDK, be sure it has
  been installed.

...

More details can be found in this discussion.

Installing pybind11 from Source

Alternatively, we can also build pybind11 from source.

  • First clone the library from GitHub
    git clone https://github.com/pybind/pybind11.git
    
  • Then build the project
    cd pybind11
    git checkout v2.4 # I use version v2.4
    mkdir build && cd build
    cmake ..
    make
    
  • Finally, if make is successful, install the library:
    make install
    

    The following messages will be displayed during installation. pybind11 headers will be in installed in /usr/local/include/pybind11/; while cmake config files are in /usr/local/share/cmake/pybind11/.

    Install the project...
    -- Install configuration: "MinSizeRel"
    -- Installing: /usr/local/include/pybind11
    -- Installing: /usr/local/include/pybind11/attr.h
    -- Installing: /usr/local/include/pybind11/embed.h
    -- Installing: /usr/local/include/pybind11/numpy.h
    -- Installing: /usr/local/include/pybind11/pybind11.h
    -- Installing: /usr/local/include/pybind11/operators.h
    -- Installing: /usr/local/include/pybind11/iostream.h
    -- Installing: /usr/local/include/pybind11/chrono.h
    -- Installing: /usr/local/include/pybind11/stl_bind.h
    -- Installing: /usr/local/include/pybind11/buffer_info.h
    -- Installing: /usr/local/include/pybind11/options.h
    -- Installing: /usr/local/include/pybind11/functional.h
    -- Installing: /usr/local/include/pybind11/stl.h
    -- Installing: /usr/local/include/pybind11/detail
    -- Installing: /usr/local/include/pybind11/detail/typeid.h
    -- Installing: /usr/local/include/pybind11/detail/descr.h
    -- Installing: /usr/local/include/pybind11/detail/internals.h
    -- Installing: /usr/local/include/pybind11/detail/common.h
    -- Installing: /usr/local/include/pybind11/detail/class.h
    -- Installing: /usr/local/include/pybind11/detail/init.h
    -- Installing: /usr/local/include/pybind11/common.h
    -- Installing: /usr/local/include/pybind11/eval.h
    -- Installing: /usr/local/include/pybind11/cast.h
    -- Installing: /usr/local/include/pybind11/eigen.h
    -- Installing: /usr/local/include/pybind11/pytypes.h
    -- Installing: /usr/local/include/pybind11/complex.h
    -- Installing: /usr/local/share/cmake/pybind11/pybind11Config.cmake
    -- Installing: /usr/local/share/cmake/pybind11/pybind11ConfigVersion.cmake
    -- Installing: /usr/local/share/cmake/pybind11/FindPythonLibsNew.cmake
    -- Installing: /usr/local/share/cmake/pybind11/pybind11Tools.cmake
    -- Installing: /usr/local/share/cmake/pybind11/pybind11Targets.cmake