Important: Please read the Qt Code of Conduct -

QProcess calling python script

  • I'm trying to launch a python script from a QT project using QProcess and It appears I keep getting an error saying


    Debugging starts
    Python error: "ImportError: No module named site\r\n"  
    Python result= ""
    Debugging has finished

    However I've broken this down to such a simple project. I am completely dumb-founded on why this is happening. I've search for a few hours here online and found some links on SO suggesting i set env paths, which didn't help at either. Any help would be appreciated.

    Here are my project files and specs:

    • Local Python Install: 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
    • list itemComputer: Windows 7 Pro
    • list itemQt Creator 4.2: Building with Desktop Qt 5.7.1 MinGW 32 Bit
    This is the python file that the QT project should be calling

    print('hello world')

    I simply created a new project add here is the mainwindow.cpp code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QProcess>
    #include <QDebug>
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        delete ui;
    void MainWindow::on_pushButton_clicked()
        QProcess p;
        QStringList params;
        QString pythonPath = "C:/Python27/python.exe";
        QString pythonScript = "C:/Users/Martini/Desktop/trash/";
        params << pythonScript;
        p.start(pythonPath, params);
        QString p_stdout = p.readAll();
        QString p_stderr = p.readAllStandardError();
           qDebug()<<"Python error:"<<p_stderr;
        qDebug()<<"Python result="<<p_stdout;

  • Lifetime Qt Champion


    Did you check whether the environment variables like PYTHONPATH are properly set ?

  • how to do same process in Ubuntu - linux ?

  • @DMT129
    Just run python scripts via QProcess on python3 .... What's the issue? If you have one you should start a new topic describing your problem.

Log in to reply