Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess calling python script
Forum Updated to NodeBB v4.3 + New Features

QProcess calling python script

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JokerMartiniJ Offline
    JokerMartiniJ Offline
    JokerMartini
    wrote on last edited by
    #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)]
    • list itemComputer: Windows 7 Pro
    • list itemQt 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;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • D Offline
        D Offline
        DMT129
        wrote on last edited by
        #3

        how to do same process in Ubuntu - linux ?

        JonBJ 1 Reply Last reply
        0
        • D DMT129

          how to do same process in Ubuntu - linux ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved