Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > call one python script from within another

Reply
Thread Tools

call one python script from within another

 
 
Tubby Tudor
Guest
Posts: n/a
 
      07-11-2003
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!

 
Reply With Quote
 
 
 
 
Irmen de Jong
Guest
Posts: n/a
 
      07-11-2003
Tubby Tudor wrote:
> 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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Fran=E7ois_Pinard?=
Guest
Posts: n/a
 
      07-11-2003
[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.

--
François Pinard http://www.iro.umontreal.ca/~pinard

 
Reply With Quote
 
hokiegal99
Guest
Posts: n/a
 
      07-12-2003
Irmen de Jong <(E-Mail Removed)> wrote in message news:<3f0f40b9$0$49103$(E-Mail Removed)4all.nl>...
> Tubby Tudor wrote:
> > 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?



Yes, this works. Thanks for the tip!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a script from another script and other script does notdo busy wait. Rajat Python 3 01-08-2010 02:05 PM
Call script which accepts com. line par. from another script anderror control Karim Ali Python 1 05-24-2007 12:21 AM
executing a python script from another python script. sarmin kho Python 1 06-04-2004 01:12 PM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM
Passing value from one script on one page to another script on another page. Robert Cohen ASP General 3 07-15-2003 01:46 PM



Advertisments