1

I have two different installations of Python (one is inside a chrooted jail, one outside, and they have different versions). From a Python script running with one of them, I need to call "the other Python" and get some internal value (sys.path).

I can use something like

subprocess.call("<the other python> -c 'import sys; print sys.path'", shell=True)

redirect it to a file, read it from there, etc. but that seems convoluted, to go through IO like that.

Is there an easy direct way?

(Basically, I need to append sys.path from one Python to another Python's sys.path).

Mark Galeck
  • 6,155
  • 1
  • 28
  • 55

1 Answers1

0

The short answer is no, there is not another more direct way. You're trying to capture run-time details from another process, so some kind of interprocess communication is necessary. The only way I can think of to get this information without running the other interpreter would be to do some very complicated parsing of configuration files and environment/shell variables, and probably also diving into the registry if you're on Windows.

I think you're much better off with a single (albeit long) one-liner:

from subprocess import Popen, PIPE

other_path = Popen("<other interpreter path> -c 'import sys; print sys.path'", 
                    stdout=PIPE, shell=True).communicate()[0]

At least with this method you're not writing anything to a disk, so you're bypassing one of the slowest IO methods.

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84