1

When I try to run the following python script from Qt I get this error. ImportError: No module named imapclient Python scripts will run just fine without the import statements. Does anyone know how to fix this? Thank you in advance for all your help. Qt doesn't seem to be using the same version of Python that is used by default on my terminal.

Qt Code (widget.cpp)

#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
#include <QTimer>
#include <QProcess>
#include <QDir>
#include <QTime>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(checkTexts()));
    timer->start(5000);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::checkTexts(){
    QProcess *process = new QProcess(this);

    QStringList arguments { QCoreApplication::applicationDirPath() + "/../../../../../Monika/Qt Desktop Application/Monika/CheckText.py"};

    process->startDetached("python", arguments);
    process->waitForFinished(-1);

    QString output = process->readAllStandardOutput();
    qDebug() << output;
    process->close();
}

Python Code (CheckText.py)

import imapclient
import imaplib

def getTexts():
        imaplib._MAXLINE = 10000000 #increases the byte limit of how much python can remember
        imapObj = imapclient.IMAPClient('imap.gmail.com', ssl=True) 
        print ("done")


getTexts()
Johnny
  • 13
  • 4
  • Is it possible that the shell you're running the Python script from does not know about your python installation? In Windows, this could happen if you don't add your python directory to the %PATH% environment variable. Can you successfully run `import imaplib` from an interactive Python session? imaplib is not part of the standard library, so you'll have to install it if you haven't already. – bfris May 15 '18 at 00:48
  • I just tried running `import imaplib` and `imaplib._MAXLINE = 10000000` from an interactive Python session and it worked. imaplib is installed. Also CheckText.py works just fine when I run it from terminal. – Johnny May 15 '18 at 00:55
  • Probably a silly question but are you sure that imapclient is installed? It needs to be installed separately. – Paula Thomas May 15 '18 at 01:04
  • @Johnny, is this Windows/Linux/Mac? On Windows, you can encounter this issue if your python directory is not in your path. In Linux, you can have two versions of Python installed (e.g. 2.7.x and 3.x.x), and if you're calling one version that doesn't have imaplib installed then that could be your problem. If you get console output from your program, maybe you could do `import sys` and `print(sys.version)` just to make sure the version matches up with what you get from terminal. – bfris May 15 '18 at 01:13
  • @bfris the versions don't match up. When I run the python script via terminal it prints 2.7.13 and when Qt runs it 2.7.10 gets printed. How do I fix this? Also I'm using Mac. – Johnny May 15 '18 at 01:21
  • @PaulaThomas how would I go about installing it separately? I just ran `pip install imapclient` once via terminal. – Johnny May 15 '18 at 03:04
  • @Johnny, check out @jersey bean's answer to [this question](https://stackoverflow.com/questions/14117945/too-many-different-python-versions-on-my-system-and-causing-problems). It seems Qt is running the system installed version of python (without imaplib installed!) but the terminal runs from a different install (homebrew?). @jersey bean's solution was to run `brew link --overwrite python` and also possibly `export PATH="/usr/local/opt/python/libexec/bin:$PATH"`. Though you probably need a better way of permanently adding the (homebrew) version to your PATH. – bfris May 15 '18 at 04:17
  • Thank you @bfris ! I couldn't figure out how to set Qt's default python to the one I had imapclient installed on so I ended up changing the QProcess start line to `process->startDetached("/Library/Frameworks/Python.framework/Versions/2.7/bin/python", arguments);` which got it working. I also changed the declaration line to `QProcess *process = new QProcess();` It now works as it should. Thank you everyone! – Johnny May 15 '18 at 06:10

0 Answers0