1

Qt Creator 4.7.1 Based on Qt 5.11.2 (Clang 8.0 (Apple), 64 bit)

I'm running this in Qt.

QProcess p;
QStringList params;
params << "/Users/johan/Documents/testQt/hello.py";
p.start("python", params);
p.waitForFinished(-1);
qDebug() << "finished";
QString p_stdout = p.readAll();
qDebug() << p_stdout;
QString p_stderr = p.readAllStandardError();
if(!p_stderr.isEmpty())
   qDebug()<<"Python error:"<<p_stderr;

I at first had the same error as this: Qt calling python using QProcess

Python error: "ImportError: No module named site\r\n"  

And I added:

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PYTHONPATH", "/Users/johan/anaconda3/lib/python3.7");
env.insert("PYTHONHOME", "/Users/johan/anaconda3/bin/python");
p.setProcessEnvironment(env);

I can directly run the python script from terminal with python hello.py. /Users/johan/anaconda3/bin/python is the output of which python. I suppose I have the correct path for PYTHONHOME, but I'm still getting error.

Python error: "  File \"/Users/johan/anaconda3/lib/python3.7/site.py\", line 177\n    file=sys.stderr)\n        ^\nSyntaxError: invalid syntax\n"

now this is the same error as this: Using multiple versions of Python

But adding what's suggested #!python3 in the script didn't help. I've also tried #!/Users/johan/anaconda3/bin/python.

After searching for hours, now I really don't know how to solve this. How do I specify to run with Python 3? Any help is appreciated.

I guess it's probably still a path problem. Please kindly educate me what I don't understand about PATH in general. I do know PATH is where shell looks for the executable. But why are we inserting PYTHONPATH and PYTHONHOME here instead of just adding it to PATH? What are PYTHONPATH and PYTHONHOME for? (I've read PYTHONHOME documentation but I don't understand.)

EDIT (hello.py for testing package imports):

import time
import sys
import os
import tensorflow as tf
import numpy as np
import time
import inspect
import cv2

def main():
    time.sleep(1)
    print(os.path)
    print(sys.version_info[0])
    print("hello")

if __name__ == '__main__':
    main()
12
  • show your .py ... Commented Nov 15, 2018 at 19:54
  • change p.start("python", params); to p.start("/Users/johan/anaconda3/bin/python", params); Commented Nov 15, 2018 at 19:56
  • #!/... only serves for bash Commented Nov 15, 2018 at 19:57
  • @eyllanesc I've added .py now it shows another error. ModuleNotFoundError: No module named 'tensorflow' Commented Nov 15, 2018 at 20:33
  • what is the outputs of: import sys print(sys.path) and echo $PATH and echo $PYTHONPATH? Commented Nov 15, 2018 at 20:46

1 Answer 1

0

In PYTHONPATH there must be the paths of the modules(so the minimum is site-packages) so the solution is:

env.insert("PYTHONPATH", "/Users/johan/anaconda3/lib/python3.7/site-packages")

You must also place the path of the python binary that is used:

p.start("/Users/johan/anaconda3/bin/python", params);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.