How do I know which source I’m running Python from?

Asked

Viewed 159 times

3

I would like to know how I do to know from which source I am running the Python.

For example, if I installed Python 3.5 on my machine and created a Virtualenv, how could I know, through Python itself, the origin of the execution of that binary?

For example, if I perform Python, to know the complete path of the executable, as /usr/bin/python, for example.

In the specific case, I don’t want anything related to Virtual Env, but to know by Python the binary origin path.

1 answer

5


Use sys.executable. Thus:

import sys
print(sys.executable)

# Output '/usr/bin/python'

According to the documentation of sys Python:

A string that provides the absolute path of the binary executable to the Python interpreter, on systems where this makes sense. If Python cannot recover the real path to its executable, sys.executable return an empty string or None.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.