'Python subprocess.run() does not run some terminal commands, such as arm-linux-gnueabi-gcc

I want to call gcc for ARM architecture programmatically from Python. Unfortunatelly it seems like some terminal commands cannot be run by the Python subprocess module.

In my terminal, I can run the following command just fine.

$ arm-linux-gnueabi-gcc -o /path/to/out -mcpu=arm1176jzf-s -mtune=arm1176jzf-s /path/to/out.s

However, when I try to call that command programmatically in Python, using subprocess.run(), I get an error.

Code:

output_binary = '/path/to/out'
output_assembly = 'path/to/out.s'
subprocess.run(f"arm-linux-gnueabi-gcc -o {output_binary} " +\
            f"-mcpu=arm1176jzf-s -mtune=arm1176jzf-s {output_assembly}")

Error:

FileNotFoundError: [Errno 2] No such file or directory: 'arm-linux-gnueabi-gcc -o /path/to/out -mcpu=arm1176jzf-s -mtune=arm1176jzf-s /path/to/out.s'

But I am sure it is not an arm-linux-gnueabi-gcc problem. Here's what I tried in Python interpreter:

$ python
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run("cd .. && echo hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/subprocess.py", line 493, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'cd .. && echo hello'

As you can see from above, even the simplest command like cd is not consumed by the python.

How to make Python recognise desired commands?



Sources

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

Source: Stack Overflow

Solution Source