How do I find the full path of the currently running Python interpreter from within the currently executing Python script?
Asked
Active
Viewed 4.4e+01k times
3 Answers
933
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 as `env -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 via `sys.executable`. Perhaps your OS or Python version behaves slightly differently. – kevinarpe May 22 '15 at 12:56
-
42Note that this will not return the name of the Python interpreter if Python is embedded in some application. – mic_e Jul 14 '15 at 00:30
-
3I 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` and `python3` 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 01 '20 at 02:50
-
typing ```type -a python3``` into the console gives me /usr/local/bin/python3, /usr/bin/python3. Yet running ```import sys print(sys.executable)``` gives me /usr/local/opt/python@3.8/bin/python3.8 So the paths do not match. What's up with that? – Tea Tree Aug 12 '20 at 17:30
-
1@mic_e, this may have been true in 2015, but I just tried it today and it behaves as expected (it returns the absolute file of the executable that embeds Python). – Shmuel Kamensky Oct 02 '20 at 17:20
-
1@mic_e how to find python executable when embedded in an interpreter? – Fred Zimmerman Nov 14 '20 at 18:27
14
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

famousgarkin
- 13,687
- 5
- 58
- 74
-
7
-
4It seems that `_` is set by the shell. But it need not be set, so this could give the wrong answer. – vy32 Sep 23 '15 at 00:40
-
2FYI, when in a Jupyter notebook, this gives the path to the kernel launcher script. – Mr Fooz Apr 17 '18 at 14:11
-
-
Also, running Python with `sudo` does not create the `_` environment variable. – Daniel F Dec 21 '21 at 18:55
-
1Does not work on Windows (10). The environment variable `_` is not set. – George Aug 31 '22 at 21:21
3
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:~ $

Tiago Martins Peres
- 14,289
- 18
- 86
- 145

kvivek
- 3,321
- 1
- 15
- 17
-
13
-
12Your "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 use `python3` 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 07:17
-
4Does not answer the question of "How do I find the full path of the currently running Python interpreter from within the currently executing Python script?" – Dwight J. Browne Jul 11 '21 at 14:51
-
5**Dumpster fire answer.** There's *no* deterministic relation between what the external shell considers to be `python` (i.e., the absolute filename of the `python` command in the current `${PATH}`) and the command the active Python interpreter is actually running under. *Yikes.* – Cecil Curry Nov 29 '21 at 23:19