How to Use the Pyhon script interpreter in a Windows Application

Source: Internet
Author: User

Are you interested in the practical application of the Python script interpreter? Today, our article mainly introduces how to embed the Python script interpreter in a Windows application and the specific introduction of the six practical solutions. The following is a detailed description of the article.

The '#' in the file name below is the abbreviated version number. for example, Python 2.1.1, # Will be replaced by 21. the following describes how to embed a Python script interpreter in a Windows application:

1.do not directly create pythonin your. EXE file. In Windows, Python should be introduced in the form of DLL, so that Python's DLL's can be used. (The first key ). Connect to Python ##. dll; you can connect to Python statically or dynamically. Static means to connect to python15.lib. The disadvantage is that your application cannot run when Python #. dll does not exist ..

Note: Python ###. lib is the so-called "import to Database" corresponding to python ##. dll. It only defines the connection symbol. Borland prompt: first use coff2omf.exe to convert Python #. lib to OMF format.

  • Python urce has four advantages in practical application.
  • Three steps to implement the Python RUD Function
  • Python module compilation steps
  • How to read and write configuration files in Python
  • Related to the three advantages of Python programming in practical application

Connection simplifies connection options because everything happens at runtime. Your code must use the Windows function LoadLibraryEx () to load Python #. dll. Your code must also use the Windows function GetProcAddress () to obtain the pointer, so that you can use it to operate the access routines and data (Python's c api's) in Python #. dll ). Macros can be used transparently in any C code that calls the Python's c api.

2. If you use SWIG to simplify encapsulation and interface generator), you can easily generate Python "expansion module", which allows Python to use the data and methods of applications. SWIG handles almost all the lame details for you. The result is that the C code is connected to your. exe file. You do not need to generate a DLL file, which simplifies the connection.

3. SWIG will generate an initialization function (C function) whose name is related to the name of the extension module. For example, if the module name is leo, the init function will be called initleo (). If you use the SWIG shadow class, the init function will be called initleoc (). This initialization usually hides the helper class used by the shadow class. The reason why you can connect the C code to your .exe file in step 2 is that this initialization function is equivalent to introducing the module to the second point in Python ).

4. In short, you can use the following code to initialize the Python script interpreter to attract your expansion module.

 
 
  1. #include "python.h"   
  2. ...   
  3. Py_Initialize(); // Initialize Python.   
  4. initmyAppc(); // Initialize (import) the helper class.   
  5. PyRun_SimpleString("import myApp") ; // Import the shadow class.  

5. if you use a compiler other than MSVC, there may be two problems. the compiler is used to build Py_None and python ##. dll contains a macro related to the complex data structure called _ Py_NoneStruct. Similarly, this code cannot work in a multi-compiler environment and should be replaced by the following code:

 
 
  1. return Py_BuildValue("");  

It may be automatically changed using the % typemap command of SWIG, although I have not done this. (I am a newbie to SWIG ).

6. Using the Python shell script to build a Python interpreter window in your Windows app is not a good idea, because the generated window

 
 
  1. Python##.dll.  

Problem 1: the so-called "very high-level" function based on FILE * will not work normally in multiple compiler environments; each compiler will treat the FILE structure differently. From the implementation perspective, they are very low-level functions.

Question 2: When the void function is encapsulated, SWIG generates the following code:

 
 
  1. Python##.dll.  

The port is independent of the window system of your app. However, you (or the wxPythonWindow class) should create a "local" interpreter window. It is easy to connect to the Python script interpreter. You can redirect the input/output of Python to any object that supports read/write, so that all you need is the object that contains the read () and write () methods.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.