Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Running Python script from Creator project

    General and Desktop
    qtcreator qt5.6 cpp
    3
    10
    4828
    Loading More Posts
    • 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
      user241996 last edited by A Former User

      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?

      aha_1980 1 Reply Last reply Reply Quote 0
      • aha_1980
        aha_1980 Lifetime Qt Champion @user241996 last edited by

        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 Reply Quote 0
        • U
          user241996 last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • U
            user241996 @aha_1980 last edited by

            @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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by SGaist

              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 Reply Quote 1
              • U
                user241996 @SGaist last edited by

                @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
                ..
                ..
                ..

                aha_1980 1 Reply Last reply Reply Quote 0
                • aha_1980
                  aha_1980 Lifetime Qt Champion @user241996 last edited by

                  @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 Reply Quote 1
                  • U
                    user241996 @aha_1980 last edited by

                    @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 Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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 Reply Quote 2
                      • U
                        user241996 @SGaist last edited by

                        @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 Reply Quote 3
                        • First post
                          Last post