How to get the current Python interpreter path from inside a Python script?

e-satis picture e-satis · May 8, 2011 · Viewed 24.8k times · Source

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.

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · May 8, 2011

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