Call Python in QT C++ and package and publish the software (bare machine can run)


      In order to improve the work efficiency, we need a software that can automatically generate multiple associated word documents without the tedious copy and paste work. Finally, we choose to use QT C++ to do interface and main logic program design. We use Python to write corresponding functions to word operation, and then call the python function written in QT to complete the corresponding functions. As this is the first time to use Python and also the first time to embed Python into QT, I have the following doubts in mind:

1. How Python operates word documents

2. How to use Python function in QT

3. According to the release process of QT program, will it be incompatible with Python and make the software unable to run


There are many good articles on how to use Python and how to call pthon online in QT. Here is a link to my learning reference articles. This essay mainly records the software release after calling Python language in QT.

1、 How Python operates word documents

I use the docx module to manipulate word documents in Python

Teach you how to use Python to operate word documents


Basically know how to use itdocxAfter the module, I feel that referring to the official documents will be more suitable for my own development.

  Python docx:



2、 How to call Python function in QT

 Using C + + to call Python: QT


common problem:


3、 How to publish program after QT calls Python

  According to the above two articles, the main functions of the code are almost realized. At this time, I began to worry that QT C + + program called another language, using QT windeployqt.exe The publisher only contains the environment needed by QT, and the python environment is not packaged, so it will not run on the computer without Python installed. After reading all the articles on the Internet once, it still couldn’t be solved. Finally, combined with the practice of many articles, I explored for a few days and finally solved the problem that I couldn’t run on bare metal. Let’s talk about the process of publishing


  1. Make Python environment folder

   1. This development uses Python 3.7. First, create a new folder and name it “Python 37”.


2. Open the python installation directory as follows:




    3. Copy all. DLL files, DLLs, include, lib, LIBS folder in the folder to the new one in the first stepPython37Folder, as shown in the figure below:



 4. WillPython37The folder is copied to the release folder of QT (the folder with a long name), as shown in the following figure:




    2. using py in C + + code_ The setpython home function specifies the path to the python library interpreter

Py_SetPythonHome((wchar_t *)(L"./Python37"));
    if ( !Py_IsInitialized() )
        printf("Cant Py_IsInitialized !\n");
        return ;

    Before calling Python related functions, usePy_SetPythonHome((wchar_t *)(L./Python37″))DesignatedThe location of Python’s libraries and interpreters. This is in the current directoryPython37Folder (the articles on the Internet are packed with compressed packages. I tried to use compressed packages. There was an error, andPython 37 folderandPython37 packages put together can also causePython 37 foldercan’t find)。

If you don’t have theThe location of the python 37 folder orPython37If there is a problem with the folder, the compiler will report an error.


  3. Use windeployqt.exe Package QT

   This operation also has a lot of articles on the Internet. I won’t elaborate here


  4. Copy relevant files to the published QT folder

  In the third step, I generated the demo1.2 folder, and copied the python37 folder we made in the first step, the. Py file used in the program and the python3.dll and python37.dll file in python37 to the demo1.2 folder. As shown in the figure below, the files to be copied to demo1.2 are circled


So far, packaging is complete.


4、 Summary

1. UsePy_SetPythonHome Function is necessary to specify the path of Python library and interpreter, which has not been mentioned before according to many methods on the InternetPy_SetPythonHomeFunction, if in the debug phasePy_SetPythonHome Function can be set to the installation directory of Python.

2. The disadvantage of this packaging method (making Python 37 file) is that the folder is large. It is said that pyinstaller andPy2exe first publishes the python program, and then generates the dis folder and base_ Library package instead of the one made herePython37Folder, but I never succeeded.

  3.PyImport_ImportModuleWhen an error is reported suddenly, check if there is a problem with the. Py file (it may be that the. Py file cannot be compiled because there are too many spaces after modification).

reference resources:


Recommended Today

Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]