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);
7
  • What if you use a windows path for script location? QString pythonScript = "C:\\Users\\Martini\\Desktop\\trash\\pythonTest.py"; Commented Jan 13, 2018 at 17:10
  • didn't work either, same error Commented Jan 13, 2018 at 17:19
  • What about this => stackoverflow.com/questions/5599872/… Commented Jan 13, 2018 at 17:21
  • already tried that, didn't help either Commented Jan 13, 2018 at 17:23
  • 1
    @JokerMartini. This obviously must be a problem with python envars - nothing else makes sense (given that import error). I suggest you try using setProcessEnvironment to set PYTHONHOME and PYTHONPATH. Commented Jan 13, 2018 at 18:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.