17

I'm using 2 separate scripts, Scale1.py and Scale2.py. To run them I enter sudo python Scale1.py or sudo python Scale2.py from the terminal command line. I would like to have a line in the Scale2.py script in which if I press a button, the program breaks and runs Scale1.py. Something like this, which doesn't work.

if GPIO.input(23) == False:
    break(sudo python Scale1.py)

3 Answers 3

26

os.system("sudo python scale1.py")

first you will need to import the os module

import os

I don't have a pi with me atm to test, but this comes from the second answer to this question: https://stackoverflow.com/questions/89228/calling-an-external-command-in-python

2
  • This is what I tried and it worked, thanks a lot Harry! But I did have to combine both programs first. Now the os.system("sudo python Scale3.py") simply restarts at the beginning of Scale3.py program, which is fine. I think import Scale3.py will also work. I didn't try subprocess, it does look interesting and is probably something I need to learn.
    – Rico
    Commented May 25, 2014 at 20:26
  • os.system() has been depricated in favor of subprocess, see stackoverflow.com/a/4256153/4212158
    – crypdick
    Commented Jun 28, 2018 at 17:30
12

In general, use the subprocess module

subprocess.call(["sudo","python","scale1.py"]) 

for command line calls.

An example processing the result of a subprocess call;

 result = subprocess.check_output(['sudo','service','mpd','restart'])

Subprocess replaces several older modules and functions, like os.system and os.spawn. It does a good job in sanitizing arguments, so it protects you from shell injection.

https://docs.python.org/2/library/subprocess.html

Of course to run a second python script there is no need for CLI call, you can import those.

1
  • I'm trying to learn how to issue commands from within a Python script (see here and here) and having some problems. I wonder if subprocess might be better in my case as well.
    – uhoh
    Commented Mar 14, 2018 at 8:44
6

You can use sudo as harry sib suggested, but you would have to add the user running the first script to the sudoers file.

The best way to run a python script from another python script is to import it. You should have the logic of your script in a method in the second script:

# Scale2.py
def run():
    do_first()
    do_second()
    [...]

# Run it only if called from the command line
if __name__ == '__main__':
    run()
# Scale1.py
import Scale2

if (GPIO.input(23) == False):
    Scale2.run()
2
  • 2
    +1, Since python is capable of this, it will be the cleanest answer.
    – LuWi
    Commented May 21, 2014 at 19:35
  • 1
    +1, this is the correct way to do what the OP wants and should probably be the accepted answer.
    – user8894
    Commented May 27, 2014 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.