Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Embedding PyQt script in c++
Forum Updated to NodeBB v4.3 + New Features

Embedding PyQt script in c++

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 2.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D DeadSo0ul

    Hi @JonB
    I did some debugging and figured out the program stops from the begininng when importing

    print("importing")
    from PyQt6 import QtCore, QtGui, QtWidgets
    from PyQt6.QtCore import QTimer
    import cryptocompare
    print("imports successful")
    

    My updated c++ code

    void MainMenu::on_LogIn_PB_clicked()
    {
        QProcess process;
        process.start("/usr/bin/python3", QStringList() << "/Users/boyankiovtorov/PycharmProjects/pythonProject/script/crypto.py");
    
        if (!process.waitForFinished()) {
            qDebug() << "Error: " << process.errorString();
        } else {
            qDebug() << "Process finished successfully.";
    
            // Read standard output of the process
            QByteArray outputData = process.readAllStandardOutput();
            QString outputString = QString::fromUtf8(outputData);
    
            qDebug() << "Output:" << outputString;
        }
    }
    

    output

    Process finished successfully.
    Output: "importing\n"
    

    No error is displaying no matter how much I wait since "finished successfully" has been displayed

    jeremy_kJ Offline
    jeremy_kJ Offline
    jeremy_k
    wrote on last edited by
    #4

    @DeadSo0ul said in Embedding PyQt script in c++:

        if (!process.waitForFinished()) {
            qDebug() << "Error: " << process.errorString();
        } else {
            qDebug() << "Process finished successfully.";
    
            // Read standard output of the process
            QByteArray outputData = process.readAllStandardOutput();
    

    The errors types reported by errorString() probably don't include what you're looking for.

    More likely, the desired output is written to the standard error file descriptor. Either read it explicitly with QProcess::readAllStandardError(), or set the process channel mode to QProcess::MergedChannels.

    Asking a question about code? http://eel.is/iso-c++/testcase/

    D 1 Reply Last reply
    0
    • jeremy_kJ jeremy_k

      @DeadSo0ul said in Embedding PyQt script in c++:

          if (!process.waitForFinished()) {
              qDebug() << "Error: " << process.errorString();
          } else {
              qDebug() << "Process finished successfully.";
      
              // Read standard output of the process
              QByteArray outputData = process.readAllStandardOutput();
      

      The errors types reported by errorString() probably don't include what you're looking for.

      More likely, the desired output is written to the standard error file descriptor. Either read it explicitly with QProcess::readAllStandardError(), or set the process channel mode to QProcess::MergedChannels.

      D Offline
      D Offline
      DeadSo0ul
      wrote on last edited by
      #5

      @jeremy_k
      I see the problem now. The script can not find any additional import that has been installed through pip
      Error output:

      Output: "Traceback (most recent call last):\n  File \"/Users/boyan/PycharmProjects/pythonProject/script/crypto.py\", line 9, in <module>\n    from PyQt6 import QtCore, QtGui, QtWidgets\nModuleNotFoundError: No module named 'PyQt6'\n"
      
      JonBJ 1 Reply Last reply
      0
      • D DeadSo0ul

        @jeremy_k
        I see the problem now. The script can not find any additional import that has been installed through pip
        Error output:

        Output: "Traceback (most recent call last):\n  File \"/Users/boyan/PycharmProjects/pythonProject/script/crypto.py\", line 9, in <module>\n    from PyQt6 import QtCore, QtGui, QtWidgets\nModuleNotFoundError: No module named 'PyQt6'\n"
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #6

        @DeadSo0ul
        A similar question/issue was raised as its own thread very recently, where modules installed via pip are not being found when run as a sub-process from a Qt C== application. I don't know why or how that is happening. Start by examining environment variables, does one of these tell pip where to look and is somehow different from a command line than from a Qt/C++ program?

        Of course if you normally run the PyQt6 in some kind of Python virtual environment (e.g. like anaconda) you must replicate whatever that requires if you run from your C++ program.

        D 1 Reply Last reply
        0
        • JonBJ JonB

          @DeadSo0ul
          A similar question/issue was raised as its own thread very recently, where modules installed via pip are not being found when run as a sub-process from a Qt C== application. I don't know why or how that is happening. Start by examining environment variables, does one of these tell pip where to look and is somehow different from a command line than from a Qt/C++ program?

          Of course if you normally run the PyQt6 in some kind of Python virtual environment (e.g. like anaconda) you must replicate whatever that requires if you run from your C++ program.

          D Offline
          D Offline
          DeadSo0ul
          wrote on last edited by
          #7

          @JonB
          Do you mean telling the qt/c++ application where to look for pip?
          I was thinking because when I run the python code from my IDE (PyCharm) the program makes a hidden folder named .venv where pip is located along with the downloaded through pip additional libraries. Is there any way to tell the qt/c++ program "hey here is this pip folder with all its libraries please include them"

          JonBJ 1 Reply Last reply
          0
          • D DeadSo0ul

            @JonB
            Do you mean telling the qt/c++ application where to look for pip?
            I was thinking because when I run the python code from my IDE (PyCharm) the program makes a hidden folder named .venv where pip is located along with the downloaded through pip additional libraries. Is there any way to tell the qt/c++ program "hey here is this pip folder with all its libraries please include them"

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #8

            @DeadSo0ul said in Embedding PyQt script in c++:

            when I run the python code from my IDE (PyCharm) the program makes a hidden folder named .venv where pip is located along with the downloaded through pip additional libraries.

            Then as I wondered might be the case i think this is the reason it runs from PyCharm but not from an external program. Assuming you mean PyCharm has permanently installed pip stuff in its own area then you must tell another program where that is for it to work.

            I do not now know the workings of PyCharm and virtual environments/directories. First things first: you must get your Python script run not only outside of Qt or Qt Creator but also outside of PyCharm. Have you got that working yet? Once you have it running directly from a terminal with no PyCharm involved then we can look at how to get it running from an external C++ program.

            P.S.
            It might be/you could Google for environment variable PYTHONPATH? It might be that all you need is for the directory containing your pip installed stuff to be in that variable for a Python script to find the modules? Also have a look inside PyCharm at what settings/directories it shows you have set up to run with?

            D 1 Reply Last reply
            0
            • JonBJ JonB

              @DeadSo0ul said in Embedding PyQt script in c++:

              when I run the python code from my IDE (PyCharm) the program makes a hidden folder named .venv where pip is located along with the downloaded through pip additional libraries.

              Then as I wondered might be the case i think this is the reason it runs from PyCharm but not from an external program. Assuming you mean PyCharm has permanently installed pip stuff in its own area then you must tell another program where that is for it to work.

              I do not now know the workings of PyCharm and virtual environments/directories. First things first: you must get your Python script run not only outside of Qt or Qt Creator but also outside of PyCharm. Have you got that working yet? Once you have it running directly from a terminal with no PyCharm involved then we can look at how to get it running from an external C++ program.

              P.S.
              It might be/you could Google for environment variable PYTHONPATH? It might be that all you need is for the directory containing your pip installed stuff to be in that variable for a Python script to find the modules? Also have a look inside PyCharm at what settings/directories it shows you have set up to run with?

              D Offline
              D Offline
              DeadSo0ul
              wrote on last edited by DeadSo0ul
              #9

              @JonB
              I did run the python script the same way PyCharm does it (I think)
              First I created a virtual environment with the command

              python3 -m venv /Users/boyan/Desktop/python/venv
              

              and ran the virtual environment with the command

              source /venv/bin/activate
              

              installed the libraries with pip install and ran the script with

              python3 file.py
              

              Can't I just tell qt to do these commands for me and will it work?
              Thinking now I don't change anything as the script still does not know what or where those libraries are.

              I will look into environment variable PYTHONPATH now.

              D 1 Reply Last reply
              0
              • D DeadSo0ul

                @JonB
                I did run the python script the same way PyCharm does it (I think)
                First I created a virtual environment with the command

                python3 -m venv /Users/boyan/Desktop/python/venv
                

                and ran the virtual environment with the command

                source /venv/bin/activate
                

                installed the libraries with pip install and ran the script with

                python3 file.py
                

                Can't I just tell qt to do these commands for me and will it work?
                Thinking now I don't change anything as the script still does not know what or where those libraries are.

                I will look into environment variable PYTHONPATH now.

                D Offline
                D Offline
                DeadSo0ul
                wrote on last edited by
                #10

                @JonB
                I managed to make the python script work without any virtual environment.

                I just installed pip with the commands

                curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
                
                python3 get-pip.py
                

                Now I can just pre-install the libraries I need and the script will run with the simple

                python3 file.py
                

                The question now is how to make qt use those pre-installed libraries

                JonBJ 1 Reply Last reply
                0
                • D DeadSo0ul

                  @JonB
                  I managed to make the python script work without any virtual environment.

                  I just installed pip with the commands

                  curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
                  
                  python3 get-pip.py
                  

                  Now I can just pre-install the libraries I need and the script will run with the simple

                  python3 file.py
                  

                  The question now is how to make qt use those pre-installed libraries

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #11

                  @DeadSo0ul
                  Well, Qt or QProcess does not use any libraries nor run any script. That is down to your python3 process. You need to find out (I don't know) how that locates the imports/libraries/directory or whatever. I suggested you perhaps look at or Google the PYTHONPATH environment variable?

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    Hi,

                    From old memories, look at the content of the activate script and configure your QProcess in a similar way. You likely have to modify the PATH environment variable as well as PYTHONPATH before starting your QProcess.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by
                      #13

                      In addition to using or replicating <venv>/bin/activate or <venv>/scripts/activate, the python interpreter in <venv>/bin/python knows about the virtual environment.

                      For example:

                      $ ~/venv/bin/python -c "import sys; print('\n'.join(sys.path))"
                      
                      /Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip
                      /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8
                      /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload
                      /Users/jeremy_k/venv/lib/python3.8/site-packages
                      

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      D 1 Reply Last reply
                      0
                      • jeremy_kJ jeremy_k

                        In addition to using or replicating <venv>/bin/activate or <venv>/scripts/activate, the python interpreter in <venv>/bin/python knows about the virtual environment.

                        For example:

                        $ ~/venv/bin/python -c "import sys; print('\n'.join(sys.path))"
                        
                        /Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip
                        /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8
                        /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload
                        /Users/jeremy_k/venv/lib/python3.8/site-packages
                        
                        D Offline
                        D Offline
                        DeadSo0ul
                        wrote on last edited by
                        #14

                        @jeremy_k
                        Hi again
                        Thanks everyone for helping me. I did manage to make it work by installing pip and setting the correct python executable as a parameter to the QProcess::start() function. My mistake was linking the wrong python (specifically, Microsoft's Python)

                        For anyone wondering here is my working code

                            QProcess process;
                            process.start("C:/Users/boyan/AppData/Local/Programs/Python/Python312/python.exe", QStringList() << "C:/Users/boyan/Desktop/sd.py");
                        
                            if (!process.waitForFinished()) {
                                qDebug() << "Error: " << process.errorString();
                            } else {
                                qDebug() << "Process finished successfully.";
                        
                                // Read standard output of the process
                                QByteArray outputData = process.readAllStandardError();
                                QString outputString = QString::fromUtf8(outputData);
                        
                                qDebug() << "Output:" << outputString;
                            }
                        
                        1 Reply Last reply
                        1
                        • D DeadSo0ul has marked this topic as solved on

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved