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?
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
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
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'
>>>
>>>