2

I have inputs such as latitude, longitude and frequency. I would like to run this command on a JS file :

exec(`python test.py '{"latitude":${latitude},"longitude":${longitude},"frequency":${frequency}}'`, (error, stdout, stderr) => { .... })

But when I run I have the following error :

error: Command failed: python test.py '{"latitude":48.118097,"longitude":-1.636503,"frequency":2.4}'
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    data=json.loads(sys.argv[1])
  File "C:\Users\****\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\****\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\****\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

And my python script begin with

import sys
import json
data=json.loads(sys.argv[1])

Moreover if I just run my python script without args, and write random data directly in the python script, it works. So I think it's a problem of the args written in the exec command.

Can you please help me.

6
  • What do you see if you do print(sys.argv)? Commented Feb 19, 2020 at 21:38
  • When I run the python script with my JS file I can't see the print of my python script. Commented Feb 19, 2020 at 21:39
  • You should print stdout in the callback function. Commented Feb 19, 2020 at 22:06
  • I think this is because sys.argv[1] is already a string, so that string will now also contain the leading and trailing apostrophe. Try changing your test.py script to use data=json.loads(sys.argv[1].replace("'", "")) instead. Commented Feb 19, 2020 at 22:16
  • Matt I tried your advice but it doesn't work. Commented Feb 19, 2020 at 22:44

1 Answer 1

1

Maybe add double quotes?

python test.py '\"{"latitude":48.118097,"longitude":-1.636503,"frequency":2.4}\"'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.