2

I am writing a script called deploy.py.

It needs to know the location of the Python executable. Specifically the Python executable that is being used to invoke the script in the first place. The reason is that it will generate a shell script.

This is what I have been doing so far:

import sys

if __name__ == '__main__':
   executable = sys.argv[1]
   print executable

And call the script as python deploy.py $(which python). This works. Is there a better way to do this?

Of course, I am running from a virtualenv. PYTHONPATH in sys.environ, etc. do not work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ranjith Ramachandra
  • 10,399
  • 14
  • 59
  • 96
  • I had a question. if the question is marked as a duplicate, does not get hidden from google search etc? I want to know what purpose it serves to mark a question as duplicate. – Ranjith Ramachandra Jan 08 '15 at 11:07

1 Answers1

4

The path to the current Python executable is in sys.executable:

import sys
print(sys.executable)

e.g.

PS C:\> py -c "import sys; print(sys.executable)"
C:\Python34\python.exe
Duncan
  • 92,073
  • 11
  • 122
  • 156