The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python FAQ. How to embed Python into a Windows application? Embedding Python in a C++ Project (how to)
By Guest on 7th November 2022 07:13:25 AM | Syntax: PYTHON | Views: 199



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. How to embed Python into a Windows application?
  2. ===============================================
  3.  
  4. Embedding the Python interpreter in a Windows app can be summarized as
  5. follows:
  6.  
  7. 1. Do _not_ build Python into your .exe file directly.  On Windows,
  8.    Python must be a DLL to handle importing modules that are
  9.    themselves DLL's.  (This is the first key undocumented fact.)
  10.   Instead, link to "python*NN*.dll"; it is typically installed in
  11.   "C:\Windows\System".  *NN* is the Python version, a number such as
  12.   "33" for Python 3.3.
  13.  
  14.   You can link to Python in two different ways.  Load-time linking
  15.   means linking against "python*NN*.lib", while run-time linking
  16.   means linking against "python*NN*.dll".  (General note:
  17.   "python*NN*.lib" is the so-called "import lib" corresponding to
  18.   "python*NN*.dll".  It merely defines symbols for the linker.)
  19.  
  20.   Run-time linking greatly simplifies link options; everything
  21.   happens at run time.  Your code must load "python*NN*.dll" using
  22.   the Windows "LoadLibraryEx()" routine.  The code must also use
  23.   access routines and data in "python*NN*.dll" (that is, Python's C
  24.    API's) using pointers obtained by the Windows "GetProcAddress()"
  25.   routine.  Macros can make using these pointers transparent to any C
  26.   code that calls routines in Python's C API.
  27.  
  28. 2. If you use SWIG, it is easy to create a Python "extension module"
  29.    that will make the app's data and methods available to Python.
  30.   SWIG will handle just about all the grungy details for you.  The
  31.   result is C code that you link *into* your .exe file (!)  You do
  32.   _not_ have to create a DLL file, and this also simplifies linking.
  33.  
  34. 3. SWIG will create an init function (a C function) whose name depends
  35.   on the name of the extension module.  For example, if the name of
  36.   the module is leo, the init function will be called initleo().  If
  37.   you use SWIG shadow classes, as you should, the init function will
  38.   be called initleoc().  This initializes a mostly hidden helper
  39.   class used by the shadow class.
  40.  
  41.   The reason you can link the C code in step 2 into your .exe file is
  42.   that calling the initialization function is equivalent to importing
  43.   the module into Python! (This is the second key undocumented fact.)
  44.  
  45. 4. In short, you can use the following code to initialize the Python
  46.   interpreter with your extension module.
  47.  
  48.      #include <Python.h>
  49.      ...
  50.      Py_Initialize();  // Initialize Python.
  51.      initmyAppc();  // Initialize (import) the helper class.
  52.      PyRun_SimpleString("import myApp");  // Import the shadow class.
  53.  
  54. 5. There are two problems with Python's C API which will become
  55.    apparent if you use a compiler other than MSVC, the compiler used
  56.    to build pythonNN.dll.
  57.  
  58.    Problem 1: The so-called "Very High Level" functions that take FILE
  59.    * arguments will not work in a multi-compiler environment because
  60.    each compiler's notion of a struct FILE will be different.  From an
  61.   implementation standpoint these are very _low_ level functions.
  62.  
  63.   Problem 2: SWIG generates the following code when generating
  64.   wrappers to void functions:
  65.  
  66.      Py_INCREF(Py_None);
  67.      _resultobj = Py_None;
  68.      return _resultobj;
  69.  
  70.   Alas, Py_None is a macro that expands to a reference to a complex
  71.   data structure called _Py_NoneStruct inside pythonNN.dll.  Again,
  72.   this code will fail in a mult-compiler environment.  Replace such
  73.   code by:
  74.  
  75.      return Py_BuildValue("");
  76.  
  77.   It may be possible to use SWIG's "%typemap" command to make the
  78.    change automatically, though I have not been able to get this to
  79.    work (I'm a complete SWIG newbie).
  80.  
  81. 6. Using a Python shell script to put up a Python interpreter window
  82.   from inside your Windows app is not a good idea; the resulting
  83.   window will be independent of your app's windowing system.  Rather,
  84.    you (or the wxPythonWindow class) should create a "native"
  85.    interpreter window.  It is easy to connect that window to the
  86.    Python interpreter.  You can redirect Python's i/o to _any_ object
  87.   that supports read and write, so all you need is a Python object
  88.   (defined in your extension module) that contains read() and write()
  89.   methods.
  90.  
  91. 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
  • Recent Pastes