- How to 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 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.
- 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.
- 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.)
- 4. In short, you can use the following code to initialize the Python
- interpreter with your extension module.
- #include <Python.h>
- ...
- Py_Initialize(); // Initialize Python.
- initmyAppc(); // Initialize (import) the helper class.
- PyRun_SimpleString("import myApp"); // Import the shadow class.
- 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.
- 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
- implementation standpoint these are very _low_ level functions.
- Problem 2: SWIG generates the following code when generating
- wrappers to void functions:
- Py_INCREF(Py_None);
- _resultobj = Py_None;
- return _resultobj;
- 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:
- return Py_BuildValue("");
- 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).
- 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 wxPythonWindow class) should create a "native"
- interpreter window. It is easy to connect that window to the
- Python interpreter. You can redirect Python's i/o to _any_ object
- that supports read and write, so all you need is a Python object
- (defined in your extension module) that contains read() and write()
- methods.
- Video: Embedding Python in a C++ Project using Visual Studio
Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.
Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license