0

I have a python script that work's perfectly well on a windows OS.

I want to run the same script on a Linux/Ubuntu machine. I changed the parameters that differ, such as... paths/directories, as well as interpreter from python.exe to python3.

Nonetheless, once I run the script I get an error within a function that works just fine on windows. The function is an API call, in essence it calls another python script with pre-defined parameters in json and reads the output from the shell.

script_path =  "/home/user/some_path_here/filename"

def ReportsByDate(date = today):
    new_report = subprocess.Popen([
        "python3",
        script_path,
        "reports/list",
        json.dumps({"date": today}),
        "pretty",
    ], shell=True, stdout=subprocess.PIPE)

    output = new_report.communicate()[0]
    output = json.loads(output)

    global df
    df = pd.DataFrame(output)
    return(df)

The error I am getting is:

Traceback (most recent call last):
  File "prod_get_today.py", line 118, in <module>
    ReportsByDate()
  File "prod_get_today.py", line 72, in ReportsByDate
    output = json.loads(output)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/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)

My assumption is that either:

  1. The interpreter is not called properly, or
  2. The data from the shell should be read differently in linux vs. windows

Any ideas what might be the problem?

4
  • 2
    You could also read stderr from "communicate" and print it. Maybe this shows the problem. Commented Oct 6, 2022 at 5:19
  • 1
    @MichaelButscher OP is not capturing stderr, so it can't be read from the communicate pipe. Commented Oct 6, 2022 at 6:47
  • @AKX I assumed that the OP is capable of adjusting the code to do that but maybe I should have mentioned more details like that "stderr" argument must be set to "subprocess.PIPE" as well in the "Popen" call to make it work. Commented Oct 6, 2022 at 9:19
  • 1
    @MichaelButscher Well, without that redirection, stderr will be happily printed to the parent process's stderr anyway, so OP would see the errors anyway :) Commented Oct 6, 2022 at 9:49

1 Answer 1

3
  • If you don't need the shell to interpret your command, get rid of it interposing.
  • To find a viable python3, you could use shutil.which() – or sys.executable to use the current interpreter. (This could break with e.g. PyInstaller'd EXEs, though.)
  • You can use check_output instead of manually wrangling a Popen. (As it is, you're not seeing whether the command runs successfully, by the way, or closing the Popen handle after you're done with it. This will do that.)
    output = subprocess.check_output([
        sys.executable,  # this script's interpreter
        script_path,
        "reports/list",
        json.dumps({"date": today}),
        "pretty",
    ])

    output = json.loads(output)
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.