5

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.

3
  • 1
    try sys.executable Commented Sep 7, 2016 at 13:34
  • Thanks, great, this is exactly what I was looking for. An answer gets an upvote and an approve! Commented Sep 7, 2016 at 13:40
  • I had checked that there were no duplicate before answering, found none. Ok, answer added. Commented Sep 7, 2016 at 13:43

1 Answer 1

6

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

import sys
print(sys.executable)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.