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()
johan
  • 1,664
  • 2
  • 14
  • 30
  • show your .py ... – eyllanesc Nov 15 '18 at 19:54
  • change `p.start("python", params);` to `p.start("/Users/johan/anaconda3/bin/python", params);` – eyllanesc Nov 15 '18 at 19:56
  • `#!/...` only serves for bash – eyllanesc Nov 15 '18 at 19:57
  • @eyllanesc I've added .py now it shows another error. `ModuleNotFoundError: No module named 'tensorflow'` – johan Nov 15 '18 at 20:33
  • what is the outputs of: `import sys` `print(sys.path)` and `echo $PATH` and `echo $PYTHONPATH`? – eyllanesc Nov 15 '18 at 20:46
  • echo $PATH: /Users/johan/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/johan/Library/Python/3.7/bin – johan Nov 15 '18 at 20:52
  • to make a simple test replace the hello.py with: `import sys` `print(sys.path)` and run it with Qt. I want to see the differences – eyllanesc Nov 15 '18 at 20:52
  • echo $PYTHONPATH: (empty) – johan Nov 15 '18 at 20:53
  • from terminal: ['', '/Users/johan/anaconda3/lib/python37.zip', '/Users/johan/anaconda3/lib/python3.7', '/Users/johan/anaconda3/lib/python3.7/lib-dynload', '/Users/johan/anaconda3/lib/python3.7/site-packages', '/Users/johan/anaconda3/lib/python3.7/site-packages/aeosa'] – johan Nov 15 '18 at 20:56
  • from Qt: ['/Users/johan/Documents/testQt', '/Users/johan/anaconda3/lib/python3.7', '/Users/johan/anaconda3/bin/python/lib/python37.zip', '/Users/johan/anaconda3/bin/python/lib/python3.7', '/Users/johan/anaconda3/bin/python/lib/python3.7/lib-dynload'] – johan Nov 15 '18 at 20:57
  • use `env.insert("PYTHONPATH", "/Users/johan/anaconda3/lib/python3.7/site-packages")` – eyllanesc Nov 15 '18 at 21:00
  • That works! I changed to `env.insert("PYTHONPATH", "/Users/johan/anaconda3/lib/python3.7/site-packages")` and removed `env.insert("PYTHONHOME", "/Users/johan/anaconda3/bin/python");` – johan Nov 15 '18 at 21:17

1 Answers1

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);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241