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()