python calling python

G. David Kuhlman dkuhlman at netcom.com
Tue Aug 22 11:57:00 EDT 2000


Either (1) you are making this harder than it needs to be or (2)
you are trying to do something different from what you describe. 
If (1), then try:

import myProgram2
myProgram2.main(arg1, arg2, ...)

As a general guide, you may want to use the 

    "if __name__ == '__main':
        main()

idiom at the bottom of each of your modules.  That way any of your
modules can be run from the command line (e.g. for testing
purposes) or can be imported (so that you can use functions and
classes in them).

An additional suggestion -- Use the 'main()' function to grab
command line arguments and then pass them as arguments to another
function, say myJob(arg1, arg2, ...), that does the real work. Then
other Python code can call myJob directly.

Larry Whitley (ldw at us.ibm.com) wrote:
> I'm sure the answer to this is straightforward, but I can't find it in the
> documentation.
> 
> How does one call python from python?  For example:  If the main() in
> myProgram1.py wants to call the main() in myProgram2.py with parameters,
> what incantation should it use?
> 
> I'm trying to use ...
> 
> os.system( "python /myPath/myProgram2.py -myparm1 xxx -myparm2 yyy" )
> 
> ...from myProgram1 but it doesn't seem to work.  (It appears to re-enter
> main() in myProgram1 with myProgram2's parms).
> 
> Larry
> 
> 
> 



More information about the Python-list mailing list