Page 2 of 2

Re: how to execute "OTHER" python scripts

Posted: Thu Mar 29, 2018 3:11 pm
by yk22wong
benalb wrote:
Thu Mar 29, 2018 2:39 pm
I usually do this:

Code: Select all

cat test01.py                                                                                                                                 

def my_test():
    print("hello, test")

def main():
    my_test()

if __name__ == "__main__":
    main()
and then, can do:

Code: Select all

>>> import test01
>>> from test01 import my_test
>>> 
>>> test01.my_test()
hello, test
>>> test01.main()
hello, test
>>> 
>>> my_test()
hello, test
Thanks. Good to know another alternative

Re: how to execute "OTHER" python scripts

Posted: Wed Mar 09, 2022 1:44 pm
by electricidea
Another possibility is to load and execute the script with the exec() command:
exec(open("foo.py").read())

Re: how to execute "OTHER" python scripts

Posted: Wed Jun 22, 2022 8:47 am
by lukesky333
Is it possible to execute another script and get the output of it in a variable?

something like:

Code: Select all

subprocess.getoutput('echo xyzzy')