'Execute shell command from python script (RaspberryPi)

I am trying to execute this command sudo mavproxy.py from a python script on raspberrypi. I can execute this in the shell and see it load.

The code i have for my current test is:

import subprocess
subprocess.call('sudo mavproxy.py')

running this the code executes however looking at the terminal window nothing happens. So i'm not sure whether it is executing correctly. Any help would be greatly appreciated.



Solution 1:[1]

Either pass the arguments as a list:

subprocess.call(['sudo', 'mavproxy.py'])

Or use shell=True:

subprocess.call('sudo mavproxy.py', shell=True)

The documentation is pretty clear about this. How did you learn to try it that way?

Solution 2:[2]

Import the call function from the subprocess module

 from subprocess import call
 call('sudo mavproxy.py', shell=True)

Solution 3:[3]

import os

import os.path

os.system('sudo python /full/path/to/mavproxy.py')

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 John Gordon
Solution 2
Solution 3 EstevaoLuis