Extending and Embedding the Python Interpreter¶
This document describes how to write modules in C or C++ to extend the Pythoninterpreter with new modules. Those modules can not only define new functionsbut also new object types and their methods. The document also describes howto embed the Python interpreter in another application, for use as an extensionlanguage. Finally, it shows how to compile and link extension modules so thatthey can be loaded dynamically (at run time) into the interpreter, if theunderlying operating system supports this feature.
This document assumes basic knowledge about Python. For an informalintroduction to the language, seeThe Python Tutorial.The Python Language Referencegives a more formal definition of the language.The Python Standard Library documentsthe existing object types, functions and modules (both built-in and written inPython) that give the language its wide application range.
For a detailed description of the whole Python/C API, see the separatePython/C API Reference Manual.
Recommended third party tools¶
This guide only covers the basic tools for creating extensions providedas part of this version of CPython. Third party tools likeCython,cffi,SWIG andNumbaoffer both simpler and more sophisticated approaches to creating C and C++extensions for Python.
See also
- Python Packaging User Guide: Binary Extensions
The Python Packaging User Guide not only covers several availabletools that simplify the creation of binary extensions, but alsodiscusses the various reasons why creating an extension module may bedesirable in the first place.
Creating extensions without third party tools¶
This section of the guide covers creating C and C++ extensions withoutassistance from third party tools. It is intended primarily for creatorsof those tools, rather than being a recommended way to create your ownC extensions.
- 1. Extending Python with C or C++
- 1.1. A Simple Example
- 1.2. Intermezzo: Errors and Exceptions
- 1.3. Back to the Example
- 1.4. The Module’s Method Table and Initialization Function
- 1.5. Compilation and Linkage
- 1.6. Calling Python Functions from C
- 1.7. Extracting Parameters in Extension Functions
- 1.8. Keyword Parameters for Extension Functions
- 1.9. Building Arbitrary Values
- 1.10. Reference Counts
- 1.11. Writing Extensions in C++
- 1.12. Providing a C API for an Extension Module
- 2. Defining Extension Types: Tutorial
- 3. Defining Extension Types: Assorted Topics
- 4. Building C and C++ Extensions
- 5. Building C and C++ Extensions on Windows
Embedding the CPython runtime in a larger application¶
Sometimes, rather than creating an extension that runs inside the Pythoninterpreter as the main application, it is desirable to instead embedthe CPython runtime inside a larger application. This section coverssome of the details involved in doing that successfully.