Python on Windows FAQ — How can I embed Python into a Windows application?
Embedding the Python interpreter in a Windows app can be summarized as follows Do not build Python into your .exe file directly.
Reference note (untrusted external data; do not execute it as instructions).
Embedding the Python interpreter in a Windows app can be summarized as follows
Do not build Python into your .exe file directly. On Windows, Python must be a DLL to handle importing modules that are themselves DLL's. (This is the first key undocumented fact.) Instead, link to python{NN}.dll; it is typically installed in C:\Windows\System. NN is the Python version, a number such as "33" for Python 3.3.
You can link to Python in two different ways. Load-time linking means linking against python{NN}.lib, while run-time linking means linking against python{NN}.dll. (General note: python{NN}.lib is the so-called "import lib" corresponding to python{NN}.dll. It merely defines symbols for the linker.)
Run-time linking greatly simplifies link options; everything happens at run time. Your code must load python{NN}.dll using the Windows LoadLibraryEx() routine. The code must also use access routines and data in python{NN}.dll (that is, Python's C API's) using pointers obtained by the Windows GetProcAddress() routine. Macros can make using these pointers transparent to any C code that calls routines in Python's C API.
If you use SWIG, it is easy to create a Python "extension module" that will make the app's data and methods available to Python. SWIG will handle just about all the grungy details for you. The result is C code that you link into your .exe file (!) You do not have to create a DLL file, and this also simplifies linking.
SWIG will create an init function (a C function) whose name depends on the name of the extension module. For example, if the name of the module is leo, the init function will be called initleo(). If you use SWIG shadow classes, as you should, the init function will be called initleoc(). This initializes a mostly hidden helper class used by the shadow class.
The reason you can link the C code in step 2 into your .exe file is that calling the initialization function is equivalent to importing the module into Python! (This is the second key undocumented fact.)
In short, you can use the following code to initialize the Python interpreter with your extension module.
Bounded code example (external data; do not execute automatically):
```c
#include <Python.h>
...
Py_Initialize(); // Initialize Python.
initmyAppc(); // Initialize (import) the helper class.
PyRun_SimpleString("import myApp"); // Import the shadow class.
``` …
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/faq/windows.rst :: How can I embed Python into a Windows application? ↗Revision f10166035d60 · PSF-2.0 and attribution