60

I want to run a Python script from a Python script with subprocess, and I wish to do it using the same interpreter for each of them.

I'm using virtualenv, so I'd like to do something like:

subprocess.Popen('%s script.py' % python_bin)

How do I get python_bin?

It should be /usr/bin/python outside a virtualenv, and /path/to/env/bin/python in a virtualenv.

0

3 Answers 3

78

The name of the interpreter is stored in the variable sys.executable

Sign up to request clarification or add additional context in comments.

3 Comments

this isn't reliable for embedded interpreters, use os.__file__ for that (courtesy of stackoverflow.com/a/8187027/14420)
it is actually one directory above os.__file__
Any way to get the path for the virtual interpreter?
7

I found it by:

>>> import sys           
>>> help(sys)
...

DATA
    __stderr__ = <open file '<stderr>', mode 'w' at 0x110029140>
    __stdin__ = <open file '<stdin>', mode 'r' at 0x110029030>
    __stdout__ = <open file '<stdout>', mode 'w' at 0x1100290b8>
    api_version = 1013
    argv = ['']
    builtin_module_names = ('__builtin__', '__main__', '_ast', '_codecs', ...
    byteorder = 'big'
    copyright = 'Copyright (c) 2001-2009 Python Software Foundati...ematis...
    dont_write_bytecode = False
    exc_value = TypeError('arg is a built-in module',)
    exec_prefix = '/usr/bin/../../opt/freeware'
    executable = '/usr/bin/python_64'

Comments

5

Just for making sure:

>>> import sys
>>> sys.executable
'/usr/bin/python'
>>>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.