Skip to main content

A minimal C++ extension using pybind11 and vcpkg

Project description

Python C++ extension

A template for a standalone C++ library with dependencies managed by vcpkg accessible through Python using pybind11.

Why should I use this template?

  • You want to write a C++ library that can be accessed through Python.
  • You want to use cmake to build your C++ code.
  • You want to use pybind11 to expose your C++ library as a Python module.
  • You want to use some C++ dependencies and manage them with vcpkg. Otherwise you should check other scikit-build sample projects.
  • You are not specially concerned about build and install optimizations, it is not a problem if they are long running.

If you want to distribute your extension using pip or conda and you mind that your users take a long time to install it, then it might be better to distribute some built binaries instead of optimizing the build process. This template might still be useful for you as it has a CD workflow for building python wheels with cibuildwheel and uploading them to PyPI.

Example usage

Create a clean Python virtual environment

python -m venv venv

Activate it on Windows

.\venv\Scripts\activate

otherwise

source ./venv/bin/activate

Install this project

pip install git+https://github.com/esdandreu/python-extension-cpp

It will take a while to build as it will build the C++ dependencies as well, but it will work. It is definitely not the most optimal way of installing a package as we are installing as well the vcpkg package manager and building from source dependencies that might as well be installed on the system. But this allows a fast development environment where adding or removing C++ dependencies should be easy.

Alternatively, you can install the package from the binaries distributed in PyPI using the continuous deployment workflow.

pip install example-python-extension-cpp

Test that the C++ code is working in the Python package

Our simple project contains a add function that adds two numbers together.

python -c "import my_python_api; print(my_python_api.add(1, 2))"

It also makes use of the C++ library fftw3 that is available through vcpkg

in order to perform a Fast Fourier Transform over a generated signal, printing its results.

python -c "import my_python_api; my_python_api.hello_fft()"

Setup

Install the requirements

Install vcpkg requirements with the addition of cmake and Python. It could be summarized as:

  • git
  • Build tools (Visual Studio on Windows or gcc on Linux for example)
  • cmake
  • Python. Make sure to have development tools installed (python3.X-dev on Linux, being X your version of Python).

If running on a clean linux environment (like a container or Windows Subsystem for Linux) you will need to install some additional tools as it is stated in vcpkg .

sudo apt-get install build-essential curl zip unzip tar pkg-config libssl-dev python3-dev

CMake

Follow the official instructions.

The required cmake version is quite high, if you are using a Linux distribution and installing cmake from the repositories take into account that they might not be updated to the latest version. However there are options to install the latest version of cmake from the command line.

Make sure that when you run cmake --version the output is 3.21 or higher. The reason for this is that we are using some of the 3.21 features to install runtime dependencies (managed with vcpkg ) together with our project so they are available to Python when using its API.

Formatters

This project uses clang-format to format the C++ code. There is a .clang-format file with options that I personally like. Download clang-format as part of LLVM from the official release page.

I als recommend using yapf to format python code.

Clone this repository with vcpkg

Cone this repository with vcpkg as a submodule and navigate into it.

git clone --recursive git@github.com:esdandreu/python-extension-cpp.git
cd python-extension-cpp

Bootstrap vcpkg in Windows. Make sure you have installed the prerequisites.

.\vcpkg\bootstrap-vcpkg.bat

Or in Linux/MacOS. Make sure you have installed developer tools

./vcpkg/bootstrap-vcpkg.sh

Building

Build locally with CMake

Navigate to the root of the repository and create a build directory.

mkdir build

Configure cmake to use vcpkg .

cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE="$pwd/vcpkg/scripts/buildsystems/vcpkg.cmake"

Build the project.

cmake --build build

Build locally with Python

It is recommended to use a clean virtual environment.

scikit-build is required before running the installer, as it is the package that takes care of the installation. The rest of dependencies will be installed automatically.

pip install scikit-build git

Install the repository. By adding [test] to our install command we can install additionally the test dependencies.

pip install .[test]

Testing

Test the C++ library with Google Test

ctest --test-dir build

Test the python extension

pytest

CI/CD

This template contains a continuous integration workflow that builds and tests the C++ library and the python extension ci.yml.

It also contains a continuous deployment workflow that builds wheels and source distributions for the python extension, then creates a github release with it and uploads it to PyPI: cd.yml. That workflow requires a repository secret named PYPI_TOKEN with a PyPI API token. is activated when pushing a version tag to the repository:

git tag -a v0.0.1 -m "First release"
git push origin --tags

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

example-python-extension-cpp-0.0.3.tar.gz (4.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

example_python_extension_cpp-0.0.3-pp38-pypy38_pp73-win_amd64.whl (481.8 kB view details)

Uploaded PyPyWindows x86-64

example_python_extension_cpp-0.0.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (471.8 kB view details)

Uploaded PyPymacOS 10.9+ x86-64

example_python_extension_cpp-0.0.3-pp37-pypy37_pp73-win_amd64.whl (481.9 kB view details)

Uploaded PyPyWindows x86-64

example_python_extension_cpp-0.0.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (471.8 kB view details)

Uploaded PyPymacOS 10.9+ x86-64

example_python_extension_cpp-0.0.3-cp310-cp310-win_amd64.whl (482.4 kB view details)

Uploaded CPython 3.10Windows x86-64

example_python_extension_cpp-0.0.3-cp310-cp310-win32.whl (405.1 kB view details)

Uploaded CPython 3.10Windows x86

example_python_extension_cpp-0.0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (577.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

example_python_extension_cpp-0.0.3-cp310-cp310-macosx_10_9_x86_64.whl (471.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

example_python_extension_cpp-0.0.3-cp39-cp39-win_amd64.whl (482.4 kB view details)

Uploaded CPython 3.9Windows x86-64

example_python_extension_cpp-0.0.3-cp39-cp39-win32.whl (405.2 kB view details)

Uploaded CPython 3.9Windows x86

example_python_extension_cpp-0.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (578.3 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

example_python_extension_cpp-0.0.3-cp39-cp39-macosx_10_9_x86_64.whl (472.1 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

example_python_extension_cpp-0.0.3-cp38-cp38-win_amd64.whl (482.4 kB view details)

Uploaded CPython 3.8Windows x86-64

example_python_extension_cpp-0.0.3-cp38-cp38-win32.whl (405.1 kB view details)

Uploaded CPython 3.8Windows x86

example_python_extension_cpp-0.0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (577.8 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

example_python_extension_cpp-0.0.3-cp38-cp38-macosx_10_9_x86_64.whl (472.1 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

example_python_extension_cpp-0.0.3-cp37-cp37m-win_amd64.whl (482.3 kB view details)

Uploaded CPython 3.7mWindows x86-64

example_python_extension_cpp-0.0.3-cp37-cp37m-win32.whl (405.0 kB view details)

Uploaded CPython 3.7mWindows x86

example_python_extension_cpp-0.0.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (577.4 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

example_python_extension_cpp-0.0.3-cp37-cp37m-macosx_10_9_x86_64.whl (471.7 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

File details

Details for the file example-python-extension-cpp-0.0.3.tar.gz.

File metadata

File hashes

Hashes for example-python-extension-cpp-0.0.3.tar.gz
Algorithm Hash digest
SHA256 fe9b26e32c9151b470d9f3e3de42161aeab075b2a6753016512be512360df26a
MD5 ca87f1062934af4c170aa4d42b8ff152
BLAKE2b-256 2a5c00a05941ed4285cd8420d90b0151c218964e330b5f44f473f19bf18c88a0

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-pp38-pypy38_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-pp38-pypy38_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 5007ad8b13f63a8b8dcf8d3d00026f4a938c2721a2076d048fee30ffb88ffd01
MD5 ca3ab480a9aad1f95b16c67cc066806d
BLAKE2b-256 eb1a6c5407994ef500494a5cf530cbe734b7240213e9423c65120affc72519cb

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 95a6e50401eb72c050c3c169b46a3d4fd9921f88abed93a73074fc5bd91c4841
MD5 61868e2d0e89dc5d250fe3eebc9c03e1
BLAKE2b-256 231b5e6c65d7c34d8026750fe65011b5134329a93bc486207941c95095468f8b

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a36c7803bbf076683375f93e767f3918081693d6ff65c7339f03235dd522e538
MD5 dfc4924f989cb9a9ba2be65bc7584efa
BLAKE2b-256 a379355f45695c5e8e297c1d3b253f8aa40b20cf54fb863f3201fb8d3d070df1

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-pp37-pypy37_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-pp37-pypy37_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 98f6e654d16b70d72376b0abe251aa8130924ef23b92db4f6d7994a973a75a59
MD5 e3fcf0ce9030b656beedb7a4946eba8c
BLAKE2b-256 ef94e7f3bc6bb5125cd0574ea69f3798390c5a8f7ac9e0a8648be3633903e916

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4191fdf8facbf94a14bbb2238216f6cc50f433702281c15620dab9c787c09e2b
MD5 3308d1521a9ef5c94c84ff461eb88b78
BLAKE2b-256 e446fe463e507d326e102088f90c97e5a9a8992f49a2693e7e6283c1f14b8053

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bd642765dd7cf4d6e42afce81e1bf2315e663c8724513ce0d11f1641dce4db7c
MD5 7e74addba03653848871140b545a8624
BLAKE2b-256 f3690b5a5d7c36f2caaded09aa0aaa84248cb44874b1991e401c428ae848ca35

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1a8f2f5e8f6bf92261fc67b44fe03e5069de5c2bbbc03be0b6ce709f9e679226
MD5 c80ce03c0ca029755e33ed5870ae8b71
BLAKE2b-256 def486f1d3fbf0ef9aa8c4186eae2f68c3b3aae4fda73b2b0e8559dccc923abb

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp310-cp310-win32.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 9013577b4fba10fb91f8b33ed7219851031ea50b761b873bf886c932b14d2aa4
MD5 8e93a17ea25f3c7e835f47bc3aae1782
BLAKE2b-256 cddb294b5d77f20d5f3d5a8ce17106cef2367b87f538772c2c134b6712cb3d2d

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dbee168723355a4a41eb6361149c1750fcfccb8259df892f0ea37e6496771269
MD5 def84f59a262a16ef5c525e3e77216d2
BLAKE2b-256 9c66c5fc3e3e6c306c43d6e2951eb7fffb529b90d98481e1c0ff91ca93618722

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c778ab33c844c3f8e8b3f9cc912ddcb38869f392ac101e5b46bc62f0b805503f
MD5 4747ee921211907bca9272fa645b41f1
BLAKE2b-256 bb885ea79c60d9a1cf43fada0324f06cd8839a41fabd9995075990e94f969bd8

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 24265cec96087fbf5ef39b4416fd6d031f96bb47bc71a6b4702e6b139f33e7e9
MD5 02056227839e9a45765a43be5e3c4c02
BLAKE2b-256 344f805f9d021601b1d75fa1b88ae03913cc8de0ad0cd00e9284f2e3b2b80feb

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp39-cp39-win32.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 2d893db937069cce6a7c2038783f1f648480ef9d101cbb5e5efc94e3958d714f
MD5 4825eeb3a36757042098945f1c2fc97a
BLAKE2b-256 fdc16934a3b3dd9344e9ee8619ea699b31d9eb127f377df13f1bafaf6c1a9346

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 14cdf604fbca1c7ec8f7b24649f3b4571081a8184f17fbfc755c4392e45d92a4
MD5 fb9456bb58c69c973ddf9ed61ffdb4c4
BLAKE2b-256 aa29381090a2c115c0fe5d65192cf76cb481b1690d4cd3dd757790691d6d2e30

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 225af05773d39d5faee09d7087570b860e6afa85febea7715d5a04fd7399e272
MD5 8bec389b472fa17b8e31d47a5bf7d20a
BLAKE2b-256 2b1bc07587703b968e08ecbfb9591a4381b91b5e9f14776d072e228bf328b90d

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 b8915c05bfa0f4b47394ac194e5e5138d6c2cc44cfde4820edd99b46c72b829b
MD5 6bccab7d08885012a9c949e717521ea9
BLAKE2b-256 b747a1f372afd93bd510b45486a2e8ed4bd27b5f6803ab49f09b33a0e96e407b

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp38-cp38-win32.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 d7231a085e5ac9485c6eba18d96db5874cc80d6caa87ba3d08b75940f60e09cc
MD5 130dc0d6e45b0b85c65165f24632f171
BLAKE2b-256 ed2c31993f9a6a1c05d3b9f8623c873e34ed147d604cb88ffdbe25aee2cfecaa

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8d1217e98484e3ffe33bd1d7bccd03124c91fc2d651ca6ee92f7ae660b434ada
MD5 1e2477578ee59f20a5b90032a97b11c7
BLAKE2b-256 92346b6d15c8c28106e9b805053df771fca281dc4ed515fc54908644ba75a4a2

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c066a947ab684e1fca6c7c9dddeaa519c32302c6c1ca438999fca54d18500652
MD5 1c0b272fc86a1b114690f5abc023ca2f
BLAKE2b-256 e3ed7f0d7d28005a499cde4654c4f030ccba42ab62b6076214b731a69b117e7f

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 291edbb6fca2df8efb84684f3fffe805285c9f4ac003729695ec7b68f0751fc6
MD5 6403af1575e221bbc26f1870275a9fe9
BLAKE2b-256 581de5dd5daef8628271441af3fc72f9fa889c3ec64c4f169279fde14f524a2c

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp37-cp37m-win32.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 a05d49d9f9bef08ec1bb031a58640b1b51984820559e87b4792c085554840bff
MD5 0c159de172bce1c11464f8049a8e6801
BLAKE2b-256 d6b9c6b2165f225703af3475a8ab687a79138c54d751c892d3394b75e77ed87c

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c1b00f865d63e2db3d543172869649ace55ebc0269266b946335f1a1683311b
MD5 f8103baf35b0e90d65bec7a8f5f21264
BLAKE2b-256 9f466dd140319f1cdb20f595528a981481b5b61cb62c62e1d77656f44fcb9f96

See more details on using hashes here.

File details

Details for the file example_python_extension_cpp-0.0.3-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for example_python_extension_cpp-0.0.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 de548d98a7a0f1bfb324f706212b50070dfd9185394643d75caeb76cc0013550
MD5 fac766af063858ad50b684184c18e48c
BLAKE2b-256 99c7b237b3f1a6d3211dfa7860331e7dfbea028810074f0377c1c7a5e6278392

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page