call one python script from within another

Discussion in 'Python' started by Tubby Tudor, Jul 11, 2003.

  1. Tubby Tudor

    Tubby Tudor Guest

    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!
    Tubby Tudor, Jul 11, 2003
    #1
    1. Advertising

  2. 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
    Irmen de Jong, Jul 11, 2003
    #2
    1. Advertising

  3. [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
    =?iso-8859-1?q?Fran=E7ois_Pinard?=, Jul 12, 2003
    #3
  4. Tubby Tudor

    hokiegal99 Guest

    Irmen de Jong <> wrote in message news:<3f0f40b9$0$49103$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!
    hokiegal99, Jul 12, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,501
    BobMonk
    Jul 15, 2003
  2. loveNUNO
    Replies:
    2
    Views:
    892
    loveNUNO
    Nov 20, 2003
  3. Rob R. Ainscough
    Replies:
    3
    Views:
    309
    Rob R. Ainscough
    Jun 14, 2005
  4. Robert Cohen
    Replies:
    3
    Views:
    249
    Andrew Durstewitz
    Jul 15, 2003
  5. Replies:
    1
    Views:
    170
    Keith Keller
    Mar 21, 2006
Loading...

Share This Page