Trouver le chemin complet de l'interpréteur Python?

Comment puis-je trouver le chemin complet de l'interpréteur Python en cours d'exécution dans le script Python en cours d'exécution?

262
demandé sur Peter Mortensen 2010-04-07 06:50:25

4 réponses

sys.executable contient le chemin complet de L'interpréteur Python en cours d'exécution.

import sys

print(sys.executable)

qui est maintenant documentée ici

403
répondu Imran 2015-11-09 21:16:39

notant simplement une autre façon d'utilité douteuse, en utilisant os.environ :

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

p.ex.

$ python -c "import os; print(os.environ['_'])"
/usr/bin/python
7
répondu famousgarkin 2015-08-24 13:31:33

il y a quelques autres façons de comprendre le python actuellement utilisé dans Linux est: 1) commande which python . 2) commande command -v python 3) type python commande

de même sur Windows avec Cygwin donnera le même résultat.

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 vous êtes déjà dans le shell python. Essayez n'importe qui de ces. Note: Ceci est une autre manière. Pas la meilleure façon pythonique.

>>>
>>> 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
répondu kvivek 2016-12-13 11:28:38

Try the WHOIS command:

whereis python
-2
répondu RaMs_YearnsToLearn 2013-10-27 19:33:29