[Tutor] Running multiple python scripts from one python scripts

Alan Gauld alan.gauld at yahoo.co.uk
Thu Oct 24 18:58:34 EDT 2019



On 24 October 2019, at 19:23, ose micah <osaosemwe at yahoo.com> wrote:

>Hello Alan, 

Hello. But I should point out that you are sending these emails to the entire tutor mailing list - currently about 430 members - not just me!

>
>I 
>
># running the scripts in processes
>
>extProc1 = sp.Popen([sys.executable, '/tmp/delete_line3.py'],  stdout=sp.PIPE)
>
>time.sleep(5)


You are doing something incredibly weird here.
Why do you not run the python code directly within your program? Just import the file as a module and call it as needed. This is much simpler and an order of magnitude faster and more efficient. eg:

import mymodule

mymodule.main().

That's precisely what Python's import module system is for.

All you need to do is follow standard python practice of putting your module code inside a function. At the end of the module out the usual python mantra

If __name__ == "__main__" : main(),

If you also want to use the script as a stand alone
program.

If the function takes arguments then extract them from sys.argv in the if name clause and make main take an argument. Then in your code it becomes

Import mymodule
mymodule.main(argument)

It's so much simpler and more powerful than messing about with subprocesses and os.system.

Alan g.


More information about the Tutor mailing list