'Unable to call subprocess with command that works from cli [duplicate]

The following command works fine from the CLI:

bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r /path/coverage.xml

When I pass the same command to subprocess with shell=False, I get the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r /path/coverage.xml'

with shell=True, I get this error:

/bin/sh: -c: line 0: syntax error near unexpected token `('

To me, it looks like the entire command is being treated as a path when shell is False, and I don't understand why subprocess thinks there is a syntax error when shell=True.

How can I get this command to work from Python?



Solution 1:[1]

The command you are trying to run requires Bash, but subprocess runs sh unless you tell it otherwise.

p = subprocess.run(
    "bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r /path/coverage.xml",
    shell=True, check=True, text=True,
    executable="/bin/bash")

needlessly runs bash as a subprocess of itself, but gets the job done here and now.

A more efficient solution would avoid the extra shell, but then perhaps you should refactor to replace curl with a simple native Python web client instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1