Bootstrap FreeKB - Python (Scripting) - Return full path to the Python interpreter using sys.executable
Python (Scripting) - Return full path to the Python interpreter using sys.executable

Updated:   |  Python (Scripting) articles

Here is how you can print the full path to the Python interpreter.

#!/usr/bin/python3
import sys
print(f"Python interpreter = {sys.executable}")

 

Which should print something like this.

Python interpreter = /usr/bin/python

 

Or like this.

Python interpreter = /usr/bin/python3

 

Take for example this Python script.

import sys
import subprocess
command = sys.executable+" --version"
stdout, stderr = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
stdout = stdout.decode('utf-8').strip()
stderr = stderr.decode('utf-8').strip()
if stdout:
  print(command +" = " +stdout)
if stderr:
  print(command +" = " +stderr)

 

Running this script should return something like this.

~]$ python testing.py 
/usr/bin/python --version = Python 2.7.5

~]$ python3 testing.py 
/usr/bin/python3 --version = Python 3.6.8

~]$ /usr/local/bin/python3.12.0/bin/python3.12 testing.py 
/usr/local/bin/python3.12.0/bin/python3.12 --version = Python 3.12.0

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c63081 in the box below so that we can be sure you are a human.