Embedding Python
If you have an application already written in C or C++ (or any other classic compiled language), you may want to embed Python as your application’s scripting language. To embed Python in languages other than C, the other language must be able to call C functions. In the following, I cover only the C view of things, since other languages vary widely regarding what you have to do in order to call C functions from them.
Installing Resident Extension Modules
In order for Python scripts to
communicate with your application, your application must supply
extension modules with Python-accessible functions and classes that
expose your application’s functionality. If these
modules are linked with your application rather than residing in
dynamic libraries that Python can load when necessary, register your
modules with Python as additional built-in modules by calling the
PyImport_AppendInittab C API function.
Setting Arguments
You may want to set the program name
and arguments, which Python scripts can access as
sys.argv, by calling either or both of the
following C API functions.
Python Initialization and Finalization
After installing extra built-in modules and optionally setting the program name, your application initializes Python. At the end, when Python is no longer needed, your application finalizes Python. The relevant functions in the C API are as follows.
Running Python Code
Your application can run Python source code from a character string or from a file. To run or ...