Build an environment to execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people)

I will explain how to build an environment that allows you to call C ++ functions from Python using Pybind11. There weren't many articles for people using Windows and Visual Studio Code, so I'll summarize what I've researched. However, the editor does not have to be Visual Studio Code.

0. Environment

We have confirmed in the following environment. CMake The following installation method is explained in this article.

1. Environment construction

1.1. Installing CMake

CMake is a tool that makes the settings for building C ++ etc. available to various compilers. However, I'm not sure about it myself, so please see here for details.

Download and install the Windows win64-x64 Installer from the Download Page (https://cmake.org/download/).

1.2. Installing Mingw-w64

Mingw-w64 is the Windwos 64bit version of the free C ++ compiler g ++. I'm not sure about this either, so please ask Mr. Google.

Download the installer etc. from the Download page. If you are in an online environment, install it with the installer MinGW-W64-install.exe. If you are in an offline environment, install and extract the 7z format file.

In this article, the placement of g ++. Exe is as follows: D: \ UserProgram \ mingw-w64 \ x86_64-8.1.0-posix-seh-rt_v6-rev0 \ mingw64 \ bin \ g ++. Exe

Next, add the above folder to the environment variable Path. To add with Powershell

Per user


> $envPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
> $envPath += ";D:\UserProgram\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin" #←g++.The path that contains the exe
> [System.Environment]::SetEnvironmentVariable("Path", $envPath, "User")

To set on a per-terminal basis, replace " User " on the first and third lines with " Macine ". In this case, you need administrator privileges.

Terminal unit (administrator authority required)


> $envPath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
> $envPath += ";D:\UserProgram\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin" #←g++.The path that contains the exe
> [System.Environment]::SetEnvironmentVariable("Path", $envPath, "Machine")

1.3. Installation of Pybind 11

If you are in an online environment, install with pip.

Powershell


> pip install pybind11

For offline environment Download pybind11-xyz-py2.py3-none-any.wh (x, y, z are numbers) from the PyPI Download Page Then install with pip.

Powershell


>pip install download destination/pybind11-2.5.0-py2.py3-none-any.whl  #2.5.Replace 0 with appropriate

I will also download the official sample for later use. If you are online (and have Git installed), go to the location you want to download on Powershell and

Powershell


git clone --recursive https://github.com/pybind/cmake_example.git

Download as. For offline environments, press the Clone or download button in the Official Sample (https://github.com/pybind/cmake_example). Also, download the pybind11 folder in the same way.

** [Important] Replace the files under cmake_example / pybind11 / include / pybind11 with the files under C: \ ProgramData \ Anaconda3 \ include \ pybind11. ** The file before replacement seems to be old and compiles, but I get a lot of warnings warning:'void PyThread_delete_key_value (int)' is deprecated [-Wdeprecated-declarations].

2. I'll try it (■ 1 ■)

From the cmake_exmple folder you downloaded earlier, copy pybind11, src, and CMakeLists.txt to an appropriate folder (hereinafter referred to as project_root).

Folder structure


project_root
├ pybind11
│ ├ docs
│ ├ include
│ │ └pybind11   #The contents of this folder
│ │ └ ・ ・ ・# C:\ProgramData\Anaconda3\include\Replace with the contents of pybind11
│ └ ・ ・ ・
├ src
│ └main.cpp
└ CMakeLists.txt

project_root/src/main.cpp


#include <pybind11/pybind11.h>

int add(int i, int j) {
    return i + j;
}

namespace py = pybind11;

PYBIND11_MODULE(cmake_example, m) {
    (Abbreviation)
    m.def("add", &add, R"pbdoc(
        Add two numbers

        Some other explanation about the add function.
    )pbdoc");
   (Abbreviation)
}

project_root/CMakeLists.txt


cmake_minimum_required(VERSION 2.8.12)
project(cmake_example)

add_subdirectory(pybind11)
pybind11_add_module(cmake_example src/main.cpp)

Open project_root in Visual Studio Code, go to the terminal withCtrl + @and do the following:

Powershell


> mkdir build                      #Create a build destination folder
> cd build                         #Move to build destination
> cmake -G "MinGW Makefiles" ..    #Configure and Generate
> cmake --build ..                 #Build

If successful, cmake_example.cp37-win_amd64.pyd will be created in the build folder. Let's try it in the previous terminal.

Powershell


> python           #Launch python
>>> from cmake_example import add 
>>> add(1,2)
3
>>> exit()         #Return to Powershell

If you can call the ʻadd` function, you are successful.

3. Conclusion

Thank you for reading to the end. I've only used the official sample in this article, but please refer to other people's articles and modify main.cpp and CMakeLists.txt. I'm not familiar with C ++, so it took me a while to be able to do just this. I would like to make my C ++ debut. Also, I'd like to investigate how to use the VS Code extension CMake Tools soon and write an article.

reference

I referred to the following article.

How to install Mingw-w64

-[Windows MinGW Development Environment](https://www.torutk.com/projects/swe/wiki/Windows_MinGW Development Environment) -[Easy] How to install MinGW-w64 [Many images]

Commentary on CMake

-Introduction to CMake -[How to use Cmake (1)](https://qiita.com/shohirose/items/45fb49c6b429e8b204ac#cmake%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6 % E3% 81% BF% E3% 82% 8B) -I tried using CMake (2) A little more decent project

How to use Pybind 11

-Official sample -Use C ++ functions from python with pybind11 -How to execute C ++ code from Python using pybind11 -Error workaround 1 -Error workaround 2 (unconfirmed)

Recommended Posts

Build an environment to execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people)
Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Environment construction
Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Debugging
Build Python development environment with Visual Studio Code
Python development environment with Windows + Anaconda3 + Visual Studio Code
Python development environment with Windows + Python + PipEnv + Visual Studio Code
Build Python3 for Windows 10 on ARM with Visual Studio 2019 (x86) on Windows 10 on ARM
Try to create a python environment with Visual Studio Code & WSL
Use C ++ functions from python with pybind11
Notes from installing Homebrew to building an Anaconda environment for Python with pyenv
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
A real way for people using python 3.8.0-2 from windows to work with multibyte characters
I tried to build an environment for machine learning with Python (Mac OS X)
Enable the virtualenv Python virtual environment for Visual Studio Code
Execute Python code from C # GUI
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)
From building a Python environment for inexperienced people to Hello world
Pass a list by reference from Python to C ++ with pybind11
How to build Python and Jupyter execution environment with VS Code
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
Build an environment for Blender built-in Python
An introduction to Python for C programmers
Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win
Create a Python development environment on Windows (Visual Studio Code remote WSL).
Steps to create a Python virtual environment with VS Code on Windows
From re-environment construction of Python to graph drawing (on visual studio code)
For beginners to build an Anaconda environment. (Memo)
How to build an environment for using multiple versions of Python on Mac
Building an Anaconda environment for Python with pyenv
Wrap C with Cython for use from Python
Character code for reading and writing csv files with python ~ windows environment ver ~
Django with Python Tools 2.2 for Visual Studio (PTVS 2.2)
Image Processing with Python Environment Setup for Windows
I customized it with Visual Studio Code (mainly for python), so I will summarize it
Settings for Python coding in Visual Studio Code
Wrap C ++ with Cython for use from Python
I want to make C ++ code from Python code!
A note I was addicted to when running Python with Visual Studio Code
From Python environment construction to virtual environment construction with anaconda
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Build a python execution environment with VS Code
A story about how Windows 10 users created an environment to use OpenCV3 with Python 3.5
Environment construction for those who want to study python easily with VS Code (for Mac)
[Cloud9] Try to build an environment with django 1.11 of Python 3.4 without understanding even 1 mm
Build an interactive environment for machine learning in Python
Preparing to use Tensorflow (Anaconda) with Visual Studio Code
App development to tweet in Python from Visual Studio 2017
Build a python environment for each directory with pyenv-virtualenv
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
Building an environment for natural language processing with Python
Make Visual Studio Code autocomplete for python external libraries
[Visual Studio Code] [Python] [Windows] Support for garbled Japanese characters in Python in VS Code task / debug output
I created an environment for Masonite, a Python web framework similar to Laravel, with Docker!
[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh
Call C language functions from Python to exchange multidimensional arrays
Rebuilding an environment for machine learning with Miniconda (Windows version)
OpenJTalk on Windows10 (Speak Japanese with Python from environment construction)
Build an environment for machine learning using Python on MacOSX
Let's get started with Python ~ Building an environment on Windows 10 ~
Create an environment for "Deep Learning from scratch" with Docker