How do I find the full path of the currently running Python interpreter from within the currently executing Python script?
sys.executable
contains full path of the currently running Python interpreter.
import sys
print(sys.executable)
which is now documented here
-
1This does not seem to work from scripts with a shebang
/usr/bin/env python
executed asenv -i ./script
. In that case it returns the current working directory. – John Freeman Apr 28 '15 at 21:50 -
2@JohnFreeman: I tried this on a GNU/Linux box w/ GNU coreutils 8.4 (env) and Python 3.4.2.
#!/usr/bin/env python3
will return the correct full binary path viasys.executable
. Perhaps your OS or Python version behaves slightly differently. – kevinarpe May 22 '15 at 12:56 -
33Note that this will not return the name of the Python interpreter if Python is embedded in some application. – mic_e Jul 14 '15 at 0:30
-
1I tried this with the shebang for python2 and python3 and it printed the correct executable. I also tried with no shebang and called the script with the
python
andpython3
commands and it printed the correct executable. – David Baucum Oct 10 '19 at 13:18 -
It doesnt seem to work, if you have created an alias for python through alternatives, i.e. alternatives --install /usr/bin/python python /usr/bin/python2 50 alternatives --install /usr/bin/python python /usr/bin/python3.5 60 alternatives --config python In this case, sys.executable still prints python2 path. – vhora Jul 1 '20 at 2:50
Just noting a different way of questionable usefulness, using os.environ
:
import os
python_executable_path = os.environ['_']
e.g.
$ python -c "import os; print(os.environ['_'])"
/usr/bin/python
There are a few alternate ways to figure out the currently used python in Linux is:
which python
command.command -v python
commandtype python
command
Similarly On Windows with Cygwin will also result the same.
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)
If you are already in the python shell. Try anyone of these. Note: This is an alternate way. Not the best pythonic way.
>>> 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'
>>>
>>>
If you are not sure of the actual path of the python command and is available in your system, Use the following command.
pi@osboxes:~ $ which python
/usr/bin/python
pi@osboxes:~ $ readlink -f $(which python)
/usr/bin/python2.7
pi@osboxes:~ $
pi@osboxes:~ $ which python3
/usr/bin/python3
pi@osboxes:~ $
pi@osboxes:~ $ readlink -f $(which python3)
/usr/bin/python3.7
pi@osboxes:~ $
-
7
-
4Your "already in the python shell" examples, all assume that the python shell started is what you get if you type
python
from the shell. If you start with an explicit different path (e.g./opt/python/2.5/bin/python
), or usepython3
and then run those python commands, all of them produced incorrect answers and that has nothing to do with not being the most pythonic way, it is just plain wrong. – Anthon Aug 24 '17 at 7:17