Launching Python from Python

Piet van Oostrum piet at cs.uu.nl
Fri Jul 27 14:28:58 EDT 2001


>>>>> Jay O'Connor <oconnor at bioreason.com> (JO) writes:

JO> All,
JO> I'm trying to launch another program from a Python program.  The other
JO> program happens to be another Python program.  I thought spawnle would
JO> be the proper place.  However, it doesn't seem to be working.

JO> 	import os
JO> 	pid = os.spawnle (os.P_NOWAIT,
JO> 			"c:\\python20\\python.exe",
JO> 			["c:\\james\\development\\python\\test.py"],
JO> 			{"PYTHONPATH":"c:\\james\\python"})

JO> my understanding is that this should spawn the other process, running
JO> the python runtime and passing in my script name.

JO> However, it doesn't quite happen.  What happens is that it launches
JO> python in interactive mode.  If I import sys, sys.path has my added path
JO> above, buy sys.argv is an array with one empty string.

JO> Any ideas why it doesn't work?  Any other ideas on how to launch another
JO> python program from within one?

There are two errors in your program:

1. spawnle doesn't have a list [args] as third parameter, but the
   parameters should be given individually. The format with the [args] is
   for spawnve.
2. You should supply an additional first argument in the argument list
   which indicated the name of the program called. It can be different from
   the filename, on Unix it is what ps displays.
   In your example python thinks it is called with no arguments.

So make it either:

pid = os.spawnle (os.P_NOWAIT,
		"c:\\python20\\python.exe",
		"python", "c:\\james\\development\\python\\test.py",
		{"PYTHONPATH":"c:\\james\\python"})

or

pid = os.spawnve (os.P_NOWAIT,
		"c:\\python20\\python.exe",
		["python", "c:\\james\\development\\python\\test.py"],
		{"PYTHONPATH":"c:\\james\\python"})

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list