tags:

views:

79

answers:

3

Hi,

I have a python script, which is executing again 4-5 python scripts. For performance reasons i want to use same interpreter for executing all the script.

Is it Possible to do that, if yes please help me out.

Thanks

Kamal

A: 

The currently executing interpreter is available in sys.executable. You can just pass that explicitly to subprocess.Popen as the first argument, or pass it as the 'executable' argument.

Thomas Wouters
sys.executable is just a string containing the path of the python program. When he says "use same interpreter", I assume he means the same process, not just the same on-disk executable.
Matthew Flaschen
+6  A: 

The obvious solution (which may require a little tweaking) is to just call the main function of each script from a master script. E.g., if script1.py contains:

#!/usr/bin/python
def main():
  // Do something
if __name__ == "__main__":
   main()

put in master.py

#!/usr/bin/python
import script1
def main():
  script1.main()

if __name__ == "__main__":
  main()

You can continue this pattern for as many scripts as you want.

Matthew Flaschen
+2  A: 

Maybe you're looking for the execfile function in Python 2.x.

In Python 3 it was removed, but there are simple alternatives.

Jason Orendorff