4

I would like to output, in my script, the full path of the Python interpreter running it:

#!/usr/bin/env python

print("{}".format(full_path_of_interpreter_running_this_script)

The script is in the PATH and run as:

script.py

Can I do that? How?

Note: Doing which python or type python in bash does not help me, because I am using pyenv, and pyenv is doing shims magic.

Note: More than identifying the Python executable, I am interested in identifying the virtualenv that is being used, and I thought knowing the full path to the interpreter will help me in this.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
blueFast
  • 37,437
  • 54
  • 182
  • 318

1 Answers1

5

This gives the full path to the command that was used to run the script:

import sys
print(sys.executable)
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jean-François Fabre
  • 131,796
  • 23
  • 122
  • 195