[Tutor] Running multiple python scripts from one python scripts
ose micah
osaosemwe at yahoo.com
Thu Oct 24 22:27:35 EDT 2019
Hello,
That was my very first deployment. Calling the scripts and running them. But, that would not work in my use case. As some of the scripts are been formed in the main scripts. As a result I cannot import a script that has not been formed.
My regards,
Ose Micah.
Sent from Yahoo Mail for iPhone
On Thursday, October 24, 2019, 6:58 PM, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
_filtered #yiv8536606287 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;}
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