Finding the right Python executable on Windows

Brian Curtin brian.curtin at gmail.com
Tue Jan 25 09:24:08 EST 2011


On Tue, Jan 25, 2011 at 04:25, Geoff Bache <geoff.bache at gmail.com> wrote:

> Hi all,
>
> I have a Python process on Windows and would like to start a Python
> subprocess using the same interpreter. I wonder how to go about this?
>
> First, I tried the obvious
>
> subprocess.Popen([ sys.executable, "subproc.py", ... ])
>
> but that fails, because my process has a "native launcher", (i.e. C:
> \path\mylauncher.exe, which is just a wrapper around the Python
> program) and hence sys.executable returns this path instead of the
> interpreter location. It isn't appropriate to use the launcher for the
> subprocess.
>
> I also tried using sys.exec_prefix but there didn't seem to be any
> standard location for the interpreter under this directory in any
> case.
>
> I feel certain there must be some way to do this as it seems a rather
> basic thing somehow, can anyone give me a hint?
>
> Regards,
> Geoff Bache


If sys.executable doesn't work due to this "native launcher", you
could try something
like this:

>>> import os
>>> import sys
>>> full_path = None
>>> for path in sys.path:
...     full = os.path.join(path, "python.exe")
...     if os.path.exists(full):
...             full_path = full
...
>>> full_path
'c:\\python31\\python.exe'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110125/c3cb918a/attachment-0001.html>


More information about the Python-list mailing list