Find full path of the Python interpreter?

How do I find the full path of the currently running Python interpreter from within the currently executing Python script?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

sys.executable contains full path of the currently running Python interpreter.

import sys

print(sys.executable)

which is now documented here

Method 2

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

Method 3

There are a few alternate ways to figure out the currently used python in Linux is:

  1. which python command.
  2. command -v python command
  3. type python command

Similarly On Windows with Cygwin will also result the same.

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5aeb0b3acb3a0ae858d8a96918b848880">[email protected]</a> ~
$ which python
/usr/bin/python

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd7c9cad5cad9d7fcf4f3efe8f2fdf1f9">[email protected]</a> ~
$ 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

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e554b4857485b557e76716d6a707f737b">[email protected]</a> ~
$ which python3
/usr/bin/python3

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="137866657a657678535b5c40475d525e56">[email protected]</a> ~
$ command -v python
/usr/bin/python

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f69d83809f80939db6beb9a5a2b8b7bbb3">[email protected]</a> ~
$ 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/pythonn'
>>>
>>> os.popen('type python').read()
'python is /usr/bin/pythonn'
>>>
>>> os.popen('command -v python').read()
'/usr/bin/pythonn'
>>>
>>>

If you are not sure of the actual path of the python command and is available in your system, Use the following command.

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d1d042d021e0f0215081e">[email protected]</a>:~ $ which python
/usr/bin/python
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e6e775e716d7c71667b6d">[email protected]</a>:~ $ readlink -f $(which python)
/usr/bin/python2.7
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a2a331a35293835223f29">[email protected]</a>:~ $ 
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c0c153c130f1e1304190f">[email protected]</a>:~ $ which python3
/usr/bin/python3
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f1f062f001c0d00170a1c">[email protected]</a>:~ $ 
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f7f664f607c6d60776a7c">[email protected]</a>:~ $ readlink -f $(which python3)
/usr/bin/python3.7
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2e371e312d3c31263b2d">[email protected]</a>:~ $


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x