1

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

Error:

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)]
  • Computer: Windows 7 Pro
  • Qt Creator 4.2: Building with Desktop Qt 5.7.1 MinGW 32 Bit

pythonTest.py: This is the python file that the QT project should be calling

print('hello world')

mainwindow.cpp: 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) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~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/pythonTest.py";

    params << pythonScript;
    p.start(pythonPath, params);
    p.waitForFinished(-1);
    QString p_stdout = p.readAll();
    QString p_stderr = p.readAllStandardError();
    if(!p_stderr.isEmpty())
       qDebug()<<"Python error:"<<p_stderr;
    qDebug()<<"Python result="<<p_stdout;
}

UPDATE (ekhumoro SOLUTION)

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PYTHONPATH", "C:\\Python27\\Lib");
env.insert("PYTHONHOME", "C:\\Python27");
p.setProcessEnvironment(env);
JokerMartini
  • 5,674
  • 9
  • 83
  • 193

0 Answers0