There are several ways to call code written in C from Python.

First, there is the ctypes module in the standard library. It allows you to load a dynamic-link library (DLL on Windows, shared libraries .so on Linux) and call functions from these libraries, directly from Python. Such libraries are usually written in C.

Second, in case of CPython there is the Python/C API. It can be used in two major ways:

A dynamic-link library can be written in C in such a way that CPython will treat it as a module (you will be able to import it in your Python programs). The Python/C API allows the library to define functions that are written in C but still callable from Python. The API is very powerful and provides functions to manipulate all Python data types and access the internals of the interpreter.

The second way to use the C API is to embed Python in a program written in C. The C program is then able to create built-in modules written in C and expose C functions, much like in the dynamic-link library approach. The API also allows the program to execute scripts which can then import and use the built-in modules. This can be used to create a Python based plug-in system.

"Bindings" are implemented either as a pure Python library using ctypes or as a dynamic-link library using Python/C API. The second option is sometimes used with tools like SWIG which make the task easier by taking care of generating the "boiler-plate" code or Boost.Python which provides a C++ API on top of Python/C API making it easier to interface with C++ code.

Further read: Foreign Function Interface

Answer from yak on Stack Overflow
๐ŸŒ
Real Python
realpython.com โ€บ python-bindings-overview
Python Bindings: Calling C or C++ From Python โ€“ Real Python
March 18, 2026 - Are you a Python developer with a C or C++ library youโ€™d like to use from Python? If so, then Python bindings allow you to call functions and pass data from Python to C or C++, letting you take advantage of the strengths of both languages.
Top answer
1 of 2
79

There are several ways to call code written in C from Python.

First, there is the ctypes module in the standard library. It allows you to load a dynamic-link library (DLL on Windows, shared libraries .so on Linux) and call functions from these libraries, directly from Python. Such libraries are usually written in C.

Second, in case of CPython there is the Python/C API. It can be used in two major ways:

A dynamic-link library can be written in C in such a way that CPython will treat it as a module (you will be able to import it in your Python programs). The Python/C API allows the library to define functions that are written in C but still callable from Python. The API is very powerful and provides functions to manipulate all Python data types and access the internals of the interpreter.

The second way to use the C API is to embed Python in a program written in C. The C program is then able to create built-in modules written in C and expose C functions, much like in the dynamic-link library approach. The API also allows the program to execute scripts which can then import and use the built-in modules. This can be used to create a Python based plug-in system.

"Bindings" are implemented either as a pure Python library using ctypes or as a dynamic-link library using Python/C API. The second option is sometimes used with tools like SWIG which make the task easier by taking care of generating the "boiler-plate" code or Boost.Python which provides a C++ API on top of Python/C API making it easier to interface with C++ code.

Further read: Foreign Function Interface

2 of 2
21

Answer is simple, python (CPython) interpreter is written in C and it can call other C libraries dynamically, your C extension module or embedded C code can be easily called from any other C code.

CPython allows special hooks so that it can call other C code or can be called from other C code. It need not even be C, any language which compiles to native code and have same calling convention.

For a simple case consider you create a program called mython, which can load any shared library and tries to call a function run e.g.

lib = dlopen("mylib.so", RTLD_LAZY);
func = dlsym(lib, "run");
(*func)();

So in way you have loaded a module and called its code, CPython does that but in more complex way, providing better interfaces and objects to pass around, plus there are other intricacies involved of memory management, thread management etc.

So platform of Python implementation must match to language in which it is being extended, e.g. CPython is not extensible in Java but Java implementation of Python called Jython can be extended in Java and similarly .NET implementation IronPython can be extended in .Net languages.

๐ŸŒ
GitHub
github.com โ€บ pybind โ€บ pybind11
GitHub - pybind/pybind11: Seamless operability between C++11 and Python ยท GitHub
pybind11 can map the following core C++ features to Python: Functions accepting and returning custom data structures per value, reference, or pointer ... CPython 3.9+, PyPy3 7.3.17+, and GraalPy 24.1+ are supported with an implementation-agnostic interface (see older versions for older CPython and PyPy versions). It is possible to bind C++11 lambda functions with captured variables.
Author: pybind
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ IntegratingPythonWithOtherLanguages
IntegratingPythonWithOtherLanguages
Tools to make C/C++ functions/methods accessible from Python by generating binding (Python extension or module) from header files.
๐ŸŒ
Kavrakilab
ompl.kavrakilab.org โ€บ python.html
Python Bindings - OMPL - Kavraki Lab
The Python bindings are generated using Nanobind which creates the ompl Python module. The main namespaces (ompl::base, ompl::control, ompl::geometric) are available as sub-modules.
๐ŸŒ
Mit
drake.mit.edu โ€บ doxygen_cxx โ€บ group__python__bindings.html
Drake: Python Bindings
All other Python code be Pythonic and use PEP 8 naming conventions. For binding functions or methods, argument names should be provided that correspond exactly to the C++ signatures using py::arg("arg_name").
๐ŸŒ
Medium
medium.com โ€บ @kapilanr2003 โ€บ a-comprehensive-walkthrough-of-python-c-binding-creation-253a6d11c1ff
A Comprehensive Walkthrough of Python/C++ Binding Creation | by Kapilan Ramasamy | Medium
August 29, 2023 - This article provides a comprehensive guide to creating Python bindings for C++ code using the pybind11 library. Python bindings enable seamless integration between C++ and Python code, allowing you to leverage C++ functionalities within your ...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ question about python bindings
r/learnpython on Reddit: Question about python bindings
February 1, 2024 -

In Linux I am installing an Analog Devices Inc "driver" called libsmu. The driver is for their Active Learning Module ADALM1000. Four paragraphs down the libsmu documentation it says, "If you want to install the python bindings..." but nowhere does it say why or what they are used for. I am only just getting acquainted with python, so I haven't learned what bindings do. Can someone give me a clue as to why they seem to be an option to install? Thanks. Edit: This page has a little more information than the documentation page above.

๐ŸŒ
DEV Community
dev.to โ€บ baduit โ€บ create-python-bindings-for-my-c-code-with-pybind11-2pck
Create Python bindings for my C++ code with PyBind11 - DEV Community
April 1, 2025 - Boost.Python which is kinda the predecessor of PyBind11 ยท Nanobind made by the creator of PyBind11, it has a similar interface, but it takes leverage of C++17 and it aims to have more efficient bindings in space and speed.
๐ŸŒ
GitHub
github.com โ€บ precice โ€บ python-bindings
GitHub - precice/python-bindings: Python language bindings for preCICE ยท GitHub
This package provides python language bindings for the C++ library preCICE. Note that the first two digits of the version number of the bindings indicate the major and minor version of the preCICE version that the bindings support. The last digit represents the version of the bindings.
Starred by 30 users
Forked by 19 users
Languages: Cython 55.0% | Python 35.5% | C++ 9.5%
๐ŸŒ
Kylebarron
kylebarron.dev โ€บ blog โ€บ fast-python-bindings
Making performant Python bindings to compiled code | Kyle Barron
October 7, 2025 - Any time a native function is called from Python, the native code needs to be able to access and operate on the Python input. For simple parameters like integers and strings, this is straightforward and something usually handled by the binding glue generated by pyo3, Cython, pybind, etc.
๐ŸŒ
MLIR
mlir.llvm.org โ€บ docs โ€บ Bindings โ€บ Python
MLIR Python Bindings - MLIR
On systems with multiple Python implementations, setting this explicitly to the preferred python3 executable is strongly recommended. ... It is highly recommended these are set to hidden, hidden, and ON (respectively) if the final built package is intended to be used in a context/use-case where multiple bindings packages will be used simultaneously (i.e., multiple bindings packages loaded in the same Python interpreter session).
๐ŸŒ
Mit
drake.mit.edu โ€บ python_bindings.html
Drake: Using Drake from Python
A substantial subset of the Drake C++ functionality is available from Python. The Drake Python bindings are generated using pybind11, which means that every function or class which is exposed to C++ has been explicitly enumerated in one of the source files inside the bindings/pydrake folder.
๐ŸŒ
Spectre-code
spectre-code.org โ€บ spectre_writing_python_bindings.html
Writing Python Bindings - SpECTRE
For example, the bindings for the DataStructures library would go in src/DataStructures/Python/. SpECTRE provides the spectre_python_add_module CMake function to make adding a new python module, be it with or without bindings, easy. The python bindings are built only if -D BUILD_PYTHON_BINDINGS=ON is passed when invoking cmake (enabled by default).
๐ŸŒ
DEV Community
dev.to โ€บ patfinder โ€บ python-cc-binding-59l6
Python C/C++ binding - Some notes - DEV Community
December 9, 2024 - Python binding is the connection between Python and external libraries or languages, allowing Python code to interface with C/C++, Java, or system-level libraries for extended functionality.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Language_binding
Language binding - Wikipedia
July 30, 2026 - In the context of software libraries, bindings are wrapper libraries that bridge two programming languages, so that a library written for one language can be used in another language. Many software libraries are written in system programming languages such as C or C++. To use such libraries from another language, usually of higher-level, such as Java, Common Lisp, Scheme, Python...
๐ŸŒ
arXiv
arxiv.org โ€บ abs โ€บ 2603.04668
[2603.04668] Python Bindings for a Large C++ Robotics Library: The Case of OMPL
March 4, 2026 - Abstract:Python bindings are a critical bridge between high-performance C++ libraries and the flexibility of Python, enabling rapid prototyping, reproducible experiments, and integration with simulation and learning frameworks in robotics research.
๐ŸŒ
ns-3
nsnam.org โ€บ wiki โ€บ Python_bindings
Python bindings - Nsnam
May 4, 2022 - Allow the programmer to write complete simulation scripts in Python; Prototype new models (e.g. routing protocols). For the time being, the primary focus of the bindings is the first goal, but the second goal will eventually be supported as well.
๐ŸŒ
Precice
precice.org โ€บ docs
Python bindings | preCICE - The Coupling Library
Use pip3 install pyprecice to install the python language bindings from PyPI