call one python script from within another

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tubby Tudor

    call one python script from within another

    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!

  • Irmen de Jong

    #2
    Re: call one python script from within another

    Tubby Tudor wrote:[color=blue]
    > 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!
    >[/color]

    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

    Comment

    • François Pinard

      #3
      Re: call one python script from within another

      [Tubby Tudor]
      [color=blue]
      > 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[/color]

      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_DESCRIPTI ON.
      """

      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_DESCRITP ION.
      """

      [...]

      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_DESCRITP ION.
      """

      [...]

      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

      Comment

      • hokiegal99

        #4
        Re: call one python script from within another

        Irmen de Jong <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote in message news:<3f0f40b9$ 0$49103$e4fe514 c@news.xs4all.n l>...[color=blue]
        > Tubby Tudor wrote:[color=green]
        > > 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!
        > >[/color]
        >
        > 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?[/color]


        Yes, this works. Thanks for the tip!

        Comment

        Working...