Embedding a Python Interpreter in a C++ Application
The process we described previously of building a DLL extension module for Python is known as extending Python; you extend its capabilities with a new module. The other common process is to put Python inside another existing application, possibly for use as a macro or extension language. This process is known as embedding Python.
If you look at the files Python installs, notice that the bulk of Python itself is implemented in a DLL, Python15.dll, for all Python 1.5.x versions. Python.exe itself is tiny, and simply uses the Python DLL. In fact, consider Python.exe as a trivial example of how to embed Python in your application.
Python exposes a large API that embedded programs use. This API is large and rich; almost anything you can do from Python code you can do from the C API, including running code, calling objects, creating new objects, pulling apart Python objects (such as getting the string value from a Python string object), and so forth.
The best way to get started with embedding Python is to read the excellent extending and embedding documentation (optionally installed with Python) in conjunction with the sample in the Demo\Embed directory of the Python sources. You can then peruse the Python/C API documentation (also optionally installed with Python).
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access