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. Running Python script from Creator project

Running Python script from Creator project

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqt5.6cpp
10 Posts 3 Posters 5.8k Views
  • 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.
  • U Offline
    U Offline
    user241996
    wrote on 10 Dec 2017, 13:09 last edited by A Former User 12 Nov 2017, 04:43
    #1

    Hi

    I am a little new to Qt Creator. I want to run a python script after a click in Qt Creator project. I also viewed this: http://www.qtcentre.org/archive/index.php/t-68641.html and it actually worked. But when I pass on an argument like a path to the JSON file, eg: "abc.json" to the following code,

        QString program( "python" );
        QStringList args = QStringList() << "read_json.py"<<"abc.json";
        QProcess p;
        p.setWorkingDirectory(WorkingDir);
        int exitCode = p.execute( program, args );
    

    It gives me the following error.

    Unexpected error: <class 'json.decoder.JSONDecodeError'>
    Traceback (most recent call last):
    File "read_json.py", line 509, in <module>
    data = json.load(JSON)
    File "/home/user/anaconda3/lib/python3.6/json/init.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
    File "/home/user/anaconda3/lib/python3.6/json/init.py", line 354, in loads
    return _default_decoder.decode(s)
    File "/home/user/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "/home/user/anaconda3/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    But when I run the same manually in terminal by typing "python read_json.py abc.json", it runs fine. Can someone give a solution to this error?

    A 1 Reply Last reply 10 Dec 2017, 14:02
    0
    • U user241996
      10 Dec 2017, 13:09

      Hi

      I am a little new to Qt Creator. I want to run a python script after a click in Qt Creator project. I also viewed this: http://www.qtcentre.org/archive/index.php/t-68641.html and it actually worked. But when I pass on an argument like a path to the JSON file, eg: "abc.json" to the following code,

          QString program( "python" );
          QStringList args = QStringList() << "read_json.py"<<"abc.json";
          QProcess p;
          p.setWorkingDirectory(WorkingDir);
          int exitCode = p.execute( program, args );
      

      It gives me the following error.

      Unexpected error: <class 'json.decoder.JSONDecodeError'>
      Traceback (most recent call last):
      File "read_json.py", line 509, in <module>
      data = json.load(JSON)
      File "/home/user/anaconda3/lib/python3.6/json/init.py", line 299, in load
      parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
      File "/home/user/anaconda3/lib/python3.6/json/init.py", line 354, in loads
      return _default_decoder.decode(s)
      File "/home/user/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
      obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/home/user/anaconda3/lib/python3.6/json/decoder.py", line 357, in raw_decode
      raise JSONDecodeError("Expecting value", s, err.value) from None
      json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

      But when I run the same manually in terminal by typing "python read_json.py abc.json", it runs fine. Can someone give a solution to this error?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 10 Dec 2017, 14:02 last edited by
      #2

      Hi @user241996,

      Just a guess, but it may be your read_json.py does not find the abc.json?

      Can you specify the /complete/path/to/abc.json?

      Qt has to stay free or it will die.

      U 1 Reply Last reply 10 Dec 2017, 15:31
      0
      • U Offline
        U Offline
        user241996
        wrote on 10 Dec 2017, 15:22 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • A aha_1980
          10 Dec 2017, 14:02

          Hi @user241996,

          Just a guess, but it may be your read_json.py does not find the abc.json?

          Can you specify the /complete/path/to/abc.json?

          U Offline
          U Offline
          user241996
          wrote on 10 Dec 2017, 15:31 last edited by
          #4

          @aha_1980 I was just telling you an example. I already specify the complete path to the read_json.py as well as the JSON file.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 Dec 2017, 21:28 last edited by SGaist 12 Oct 2017, 21:28
            #5

            Hi and welcome to devnet,

            As a simple debugging technique, did you try to print the content of the JSON file from your script ? That way you should be able to ensure that the file is properly found.

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

            U 1 Reply Last reply 11 Dec 2017, 09:12
            1
            • S SGaist
              10 Dec 2017, 21:28

              Hi and welcome to devnet,

              As a simple debugging technique, did you try to print the content of the JSON file from your script ? That way you should be able to ensure that the file is properly found.

              U Offline
              U Offline
              user241996
              wrote on 11 Dec 2017, 09:12 last edited by
              #6

              @SGaist
              Name of JSON: /home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json
              <_io.TextIOWrapper name='/home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json' mode='r' encoding='UTF-8'>
              Unexpected error: <class 'json.decoder.JSONDecodeError'>
              Traceback (most recent call last):
              File "read_json.py", line 509, in <module>
              data = json.load(JSON)
              File "/home/user/anaconda3/lib/python3.6/json/init.py", line 299, in load
              parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
              File "/home/user/anaconda3/lib/python3.6/json/init.py", line 354, in loads
              return _default_decoder.decode(s)
              File "/home/user/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
              obj, end = self.raw_decode(s, idx=_w(s, 0).end())
              File "/home/user/anaconda3/lib/python3.6/json/decoder.py", line 357, in raw_decode
              raise JSONDecodeError("Expecting value", s, err.value) from None
              json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

              When the same is run manually, it gives the following that means it runs fine. I have already debugged my file.
              user@xyz-OptiPlex-9030-AIO:~/Desktop/funcConn/funcConnGUI/Scripts$ python read_json.py /home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json
              Name of JSON: /home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json
              <_io.TextIOWrapper name='/home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json' mode='r' encoding='UTF-8'>
              Made mask for the ttest: /home/user/Desktop/FConnectivityAnl/tmp/mask_for_ttest.nii.gz
              Functional Files in this group: ['/home/user/Desktop/sub-0050952/func/sub-0050952_task-rest_run-1_bold.nii.gz', '/home/user/Desktop/sub-0050952/func/sub-0050952_task-rest_run-1_bold.nii.gz', '/home/user/Desktop/sub-0050952/func/sub-0050952_task-rest_run-1_bold.nii.gz']
              171211-14:35:47,488 workflow INFO:
              Workflow CorrCalc_group0 settings: ['check', 'execution', 'logging']
              171211-14:35:47,492 workflow INFO:
              Running in parallel.
              171211-14:35:47,494 workflow INFO:
              Executing: coff_matrix ID: 0
              171211-14:35:47,496 workflow INFO:
              Adding 3 jobs for mapnode coff_matrix
              171211-14:35:47,499 workflow INFO:
              Executing: _coff_matrix0 ID: 2
              ..
              ..
              ..

              A 1 Reply Last reply 11 Dec 2017, 09:17
              0
              • U user241996
                11 Dec 2017, 09:12

                @SGaist
                Name of JSON: /home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json
                <_io.TextIOWrapper name='/home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json' mode='r' encoding='UTF-8'>
                Unexpected error: <class 'json.decoder.JSONDecodeError'>
                Traceback (most recent call last):
                File "read_json.py", line 509, in <module>
                data = json.load(JSON)
                File "/home/user/anaconda3/lib/python3.6/json/init.py", line 299, in load
                parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
                File "/home/user/anaconda3/lib/python3.6/json/init.py", line 354, in loads
                return _default_decoder.decode(s)
                File "/home/user/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
                obj, end = self.raw_decode(s, idx=_w(s, 0).end())
                File "/home/user/anaconda3/lib/python3.6/json/decoder.py", line 357, in raw_decode
                raise JSONDecodeError("Expecting value", s, err.value) from None
                json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

                When the same is run manually, it gives the following that means it runs fine. I have already debugged my file.
                user@xyz-OptiPlex-9030-AIO:~/Desktop/funcConn/funcConnGUI/Scripts$ python read_json.py /home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json
                Name of JSON: /home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json
                <_io.TextIOWrapper name='/home/user/Desktop/FConnectivityAnl/FConnectivityAnlDesign.json' mode='r' encoding='UTF-8'>
                Made mask for the ttest: /home/user/Desktop/FConnectivityAnl/tmp/mask_for_ttest.nii.gz
                Functional Files in this group: ['/home/user/Desktop/sub-0050952/func/sub-0050952_task-rest_run-1_bold.nii.gz', '/home/user/Desktop/sub-0050952/func/sub-0050952_task-rest_run-1_bold.nii.gz', '/home/user/Desktop/sub-0050952/func/sub-0050952_task-rest_run-1_bold.nii.gz']
                171211-14:35:47,488 workflow INFO:
                Workflow CorrCalc_group0 settings: ['check', 'execution', 'logging']
                171211-14:35:47,492 workflow INFO:
                Running in parallel.
                171211-14:35:47,494 workflow INFO:
                Executing: coff_matrix ID: 0
                171211-14:35:47,496 workflow INFO:
                Adding 3 jobs for mapnode coff_matrix
                171211-14:35:47,499 workflow INFO:
                Executing: _coff_matrix0 ID: 2
                ..
                ..
                ..

                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 11 Dec 2017, 09:17 last edited by
                #7

                @user241996

                I have two further ideas:

                1. Is the same python interpreter run when you call the script from your program or from command line?
                2. The environment may be different. The script started by QProcess inherits the environment from your app. If you ran your app from QtCreator, your app inherits the environment from QtCreators run environment. Some variable like PATH or LD_LIBRARY_PATH may influence your script...

                Qt has to stay free or it will die.

                U 1 Reply Last reply 11 Dec 2017, 09:40
                1
                • A aha_1980
                  11 Dec 2017, 09:17

                  @user241996

                  I have two further ideas:

                  1. Is the same python interpreter run when you call the script from your program or from command line?
                  2. The environment may be different. The script started by QProcess inherits the environment from your app. If you ran your app from QtCreator, your app inherits the environment from QtCreators run environment. Some variable like PATH or LD_LIBRARY_PATH may influence your script...
                  U Offline
                  U Offline
                  user241996
                  wrote on 11 Dec 2017, 09:40 last edited by
                  #8

                  @aha_1980 They both call the same interpreter and the json called is also same i.e.

                  /home/user/anaconda3/bin/python
                  /home/user/anaconda3/lib/python3.6/json/init.py

                  I checked this by putting

                      print(sys.executable)
                      print(os.path.abspath(json.__file__))
                  
                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 11 Dec 2017, 12:20 last edited by
                    #9

                    Hence my suggestion to just print the content of that file and not try to load it. I'm not saying its content is wrong. It is just to ensure that your python script gets correctly called with the right parameters. I'd print the parameters received by the script, then try to just open the file and dump its content.

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

                    U 1 Reply Last reply 16 Dec 2017, 12:01
                    2
                    • S SGaist
                      11 Dec 2017, 12:20

                      Hence my suggestion to just print the content of that file and not try to load it. I'm not saying its content is wrong. It is just to ensure that your python script gets correctly called with the right parameters. I'd print the parameters received by the script, then try to just open the file and dump its content.

                      U Offline
                      U Offline
                      user241996
                      wrote on 16 Dec 2017, 12:01 last edited by
                      #10

                      @SGaist Your comment was very helpful. I checked the contents by printing them and saw that when I called the process, the file was still being written (since I write a JSON file from the GUI). So I added one more line QFile.close() and now the python script is called only after JSON gets written from the GUI. Now it is running fine.

                      1 Reply Last reply
                      3

                      4/10

                      10 Dec 2017, 15:31

                      • Login

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