O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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).

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required