Python Frequently Asked Questions, Release 3.9.5 6.6 How can I embed Python into a Windows application? Embedding the Python interpreter in a Windows app can be summarized as follows: 1. 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 rou- tines 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. Borland note: convert python NN .lib to OMF format using Coff2Omf.exe first. 2. 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. 3. 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. 4. In short, you can use the following code to initialize the Python interpreter with your extension module. 5. There are two problems with Python’s C API which will become apparent if you use a compiler other than MSVC, the compiler used to build pythonNN.dll. 6. Using a Python shell script to put up a Python interpreter window from inside your Windows app is not a good idea; the resulting window will be independent of your app’s windowing system. Rather, you (or the Problem 1: The so-called “Very High Level” functions that take FILE * arguments will not work in a multi- compiler environment because each compiler’s notion of a struct FILE will be different. From an implemen- tation standpoint these are very _low_ level functions. Problem 2: SWIG generates the following code when generating wrappers to void functions: Alas, Py_None is a macro that expands to a reference to a complex data structure called _Py_NoneStruct inside pythonNN.dll. Again, this code will fail in a mult-compiler environment. Replace such code by: It may be possible to use SWIG’s$_{%typemap}$ command to make the change automatically, though I have not been able to get this to work (I’m a complete SWIG newbie). 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.) 6.6. How can I embed Python into a Windows application? 69 #include "python.h" ... Py_Initialize(); // Initialize Python. initmyAppc(); // Initialize (import) the helper class. PyRun_SimpleString( "import myApp"); // Import the shadow class. Py_INCREF(Py_None); _resultobj=Py_None; return _resultobj; return Py_BuildValue("");