Encuentra la ruta completa del intérprete de Python?


¿Cómo puedo encontrar la ruta completa del intérprete Python que se está ejecutando desde el script Python que se está ejecutando?

 262
Author: Peter Mortensen, 2010-04-07

4 answers

sys.executable contiene la ruta completa del intérprete Python actualmente en ejecución.

import sys

print(sys.executable)

Que ahora está documentado aquí

 403
Author: Imran,
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
2015-11-09 21:16:39

Simplemente observando una forma diferente de utilidad cuestionable, usando os.environ:

import os
python_executable_path = os.environ['_']

Por ejemplo

$ python -c "import os; print(os.environ['_'])"
/usr/bin/python
 7
Author: famousgarkin,
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
2015-08-24 13:31:33

Hay algunas formas alternativas de averiguar el python actualmente utilizado en Linux es: 1) which python comando. 2) command -v python comando 3) type python comando

De manera similar en Windows con Cygwin también resultará lo mismo.

kuvivek@HOSTNAME ~
$ which python
/usr/bin/python

kuvivek@HOSTNAME ~
$ whereis python
python: /usr/bin/python /usr/bin/python3.4 /usr/lib/python2.7 /usr/lib/python3.4        /usr/include/python2.7 /usr/include/python3.4m /usr/share/man/man1/python.1.gz

kuvivek@HOSTNAME ~
$ which python3
/usr/bin/python3

kuvivek@HOSTNAME ~
$ command -v python
/usr/bin/python

kuvivek@HOSTNAME ~
$ type python
python is hashed (/usr/bin/python)

Si ya está en el shell de python. Prueba cualquiera de estos. Nota: Esta es una forma alternativa. No es el mejor python el camino.

>>>
>>> import os
>>> os.popen('which python').read()
'/usr/bin/python\n'
>>>
>>> os.popen('type python').read()
'python is /usr/bin/python\n'
>>>
>>> os.popen('command -v python').read()
'/usr/bin/python\n'
>>>
>>>
 1
Author: kvivek,
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-12-13 11:28:38

Pruebe el comando donde está:

whereis python
 -2
Author: RaMs_YearnsToLearn,
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
2013-10-27 19:33:29