call one python script from within another

T

Tubby Tudor

What is the best way to call one python script from within another
python script? For example:

one.py finishes succesfully and calls two.py which finishes OK and then
calls three.py

Thanks guys!
 
I

Irmen de Jong

Tubby said:
What is the best way to call one python script from within another
python script? For example:

one.py finishes succesfully and calls two.py which finishes OK and then
calls three.py

Thanks guys!

one.py:
---------------

import two
import three


def main():
.... body of your code here ....


main() # run code in one
two.main() # run code in two
three.main() # run code in three
------------------

Will this do?

If not, you have to explain better what exactly you want to achieve...

--Irmen
 
?

=?iso-8859-1?q?Fran=E7ois_Pinard?=

[Tubby Tudor]
What is the best way to call one python script from within another python
script? For example: one.py finishes succesfully and calls two.py which
finishes OK and then calls three.py

I do not know if my way is the best way :), but the following goes fine for
us. Each of our programs is installed (through `distutils') as a small
bootstrap, on the loader search path, looking like:

---------------------------------------------------------------------->
#!/usr/bin/env python
# [...]

"""\
SHORT_DESCRIPTION.
"""

import sys
from PACKAGE.MODULE import main
main(*sys.argv[1:])
----------------------------------------------------------------------<

and within the main MODULE for the PACKAGE holding the application, we have:

---------------------------------------------------------------------->
#!/usr/bin/env python
# [...]

"""\
LONGER_DESCRITPION.
"""

[...]

def main(*arguments):
[...]

if __name__ == '__main__':
main(*sys.argv[1:])
----------------------------------------------------------------------<

We often use this instead, when the complexity warrants it:

---------------------------------------------------------------------->
#!/usr/bin/env python
# [...]

"""\
LONGER_DESCRITPION.
"""

[...]

class Main:
[...]

def main(self, *arguments):
[...]

run = Main()
main = run.main

[...]

if __name__ == '__main__':
main(*sys.argv[1:])
----------------------------------------------------------------------<

So very systematically, one may call any `main' giving, as string arguments,
what would be successive arguments in a shell command calling that program.

When a Python program needs to "execute" another Python program, it often
does something like:

---------------------------------------------------------------------->
[...]
import PACKAGE.MODULE
PACKAGE.MODULE.main(ARGUMENT1, ARGUMENT2, ...)
----------------------------------------------------------------------<

The only reason we would prefer to `exec' or `fork' another Python process
would be to reclaim the storage taken by the imported package modules code,
in the rather unusual cases it would matter. As for the rest of the data,
we merely rely on the garbage collector.

If we were in your situation, it is likely that a small driver would
successively call the three programs as above, one after the other.
 
H

hokiegal99

Irmen de Jong said:
one.py:
---------------

import two
import three


def main():
.... body of your code here ....


main() # run code in one
two.main() # run code in two
three.main() # run code in three


Yes, this works. Thanks for the tip!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top