C++ Qt: QProcess run Python script path specifying Python version










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 sitern" 


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 177n file=sys.stderr)n ^nSyntaxError: invalid syntaxn"


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









share|improve this question
























  • 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
















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 sitern" 


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 177n file=sys.stderr)n ^nSyntaxError: invalid syntaxn"


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









share|improve this question
























  • 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














1












1








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 sitern" 


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 177n file=sys.stderr)n ^nSyntaxError: invalid syntaxn"


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









share|improve this question
















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 sitern" 


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 177n file=sys.stderr)n ^nSyntaxError: invalid syntaxn"


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






python c++ qt path






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:51







johan

















asked Nov 15 '18 at 18:17









johanjohan

136113




136113












  • 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


















  • 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

















show your .py ...

– eyllanesc
Nov 15 '18 at 19:54





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





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





#!/... 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





@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






what is the outputs of: import sys print(sys.path) and echo $PATH and echo $PYTHONPATH?

– eyllanesc
Nov 15 '18 at 20:46













1 Answer
1






active

oldest

votes


















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





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325625%2fc-qt-qprocess-run-python-script-path-specifying-python-version%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









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





    share|improve this answer



























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





      share|improve this answer

























        0












        0








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





        share|improve this answer













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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 21:24









        eyllanesceyllanesc

        84.6k103562




        84.6k103562





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325625%2fc-qt-qprocess-run-python-script-path-specifying-python-version%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto