Home > Software engineering >  Error running python script from windows to linux
Error running python script from windows to linux

Time:10-06

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?

CodePudding user response:

  • 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)
  • Related