Finding the right Python executable on Windows

Discussion in 'Python' started by Geoff Bache, Jan 25, 2011.

  1. Geoff Bache

    Geoff Bache Guest

    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
     
    Geoff Bache, Jan 25, 2011
    #1
    1. Advertising

  2. Geoff Bache

    Geoff Bache Guest

    On Tue, Jan 25, 2011 at 3:24 PM, Brian Curtin <> wrote:
    > On Tue, Jan 25, 2011 at 04:25, Geoff Bache <> 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'


    Thanks Brian!

    It never occurred to me that the Python executable would be on
    sys.path, but it seems to be on the installations I've tried on
    Windows. It isn't on other platforms of course, but it's easy enough
    to only do this on Windows.

    I wonder why it's like this? I can't see anything in that directory I
    could import...

    Regards,
    Geoff
     
    Geoff Bache, Jan 26, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    511
    Dave Thompson
    Aug 21, 2006
  2. pratap
    Replies:
    20
    Views:
    792
    Flash Gordon
    Mar 7, 2007
  3. Jason Cavett
    Replies:
    8
    Views:
    659
    Sanjay
    May 16, 2007
  4. Replies:
    0
    Views:
    465
  5. Replies:
    3
    Views:
    413
    Terry Reedy
    May 15, 2009
Loading...

Share This Page