¿Cómo obtener la ruta actual del intérprete de Python desde un script de Python? [duplicar]


Esta pregunta ya tiene una respuesta aquí:

Quiero ejecutar un script Python desde un script Python con subprocess, y quiero hacerlo usando el mismo intérprete para cada uno de ellos.

Estoy usando virtualenv, así que me gustaría hacer algo como:

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

¿Cómo consigo python_bin?

Debe estar /usr/bin/python fuera de un virtualenv, y /path/to/env/bin/python en un virtualenv.

Author: Peter Mortensen, 2011-05-08

3 answers

El nombre del intérprete se almacena en la variable sys.ejecutable

 59
Author: Ignacio Vazquez-Abrams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-08 13:54:24

Lo encontré por:

>>> 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'
 7
Author: nemo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-21 10:29:45

Solo para asegurarse:

>>> import sys
>>> sys.executable
'/usr/bin/python'
>>>
 2
Author: Yuda Prawira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-16 09:18:48