get path of python binary that's executing the script

neurino picture neurino · Mar 1, 2011 · Viewed 26.8k times · Source

I have a Pylons controller (irrelevant but explains why I have this need) say starter.py that starts another process using:

retcode = subprocess.call(('python','/path/to/myScript.py'))

now since the app runs in a virtual env python is not the right binary to call since it has no access to site-packages installed in my virtual env.

It should be instead:

retcode = subprocess.call(('path/to/virtual/env/bin/python','/path/to/myScript.py'))

and path/to/virtual/env/bin/python is the same that is running starter.py.

Any chance to not set this path (say, in a .ini file) and retrieve it?

Answer

Sven Marnach picture Sven Marnach · Mar 1, 2011

The path is available in sys.executable.