1

I have three python scripts: schedule.py, script1.py and script2.py.

I want to start running script1.py and script2.py by schedule.py. Moreover, I want to run script1.py first, and then activate script2.py after script1.py has finished.

I found a similar question here, but I think this person wants to do this via Terminal, or a similar program. I want to do this via Python.

For the moment, I have,

import script1.py
import script2.py

How do I go about?

Barry Chapman
  • 6,690
  • 3
  • 36
  • 64
LucSpan
  • 1,831
  • 6
  • 31
  • 66

2 Answers2

1

I would recommend wrapping your python scripts (script1.py, script2.py) within their own functions and then calling those functions in order from the schedule.py

flamewave000
  • 547
  • 5
  • 12
1

Well you can use the code you have in these scripts by invoking

script1.whatever_function_you_have 

Just treat them like modules. No different than importing os, sys, or any other module you create. If you want to run the entire py script, then you can always create a main loop at the bottom of the file to run the code. For example:

import script1
import script2

if __name__ == '__main__':
    script1.foobar()
    script2.goobat()

This would be placed at the bottom of the schedule file and you list what code you want to run and it will run it.

Peter Steele
  • 224
  • 3
  • 11
  • Just did a quick google search and found this as well: http://stackoverflow.com/questions/12603482/execute-multiple-python-files-using-a-single-main you might want to look a little harder next time when researching for your answers as you will invoke plenty of downvotes for lack of trying. Just a helpful tip. – Peter Steele Mar 20 '17 at 19:43