sys.executable
A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable
will be an empty string or None
.
>>> import sys >>> sys.executable 'C:\\Python310\\python.exe'
os.path.dirname(path)
Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split()
.
>>> import os >>> import sys >>> os.path.dirname(sys.executable) 'C:\\Python310'
>>> import os >>> import sys >>> os.path.dirname(sys.executable) 'C:\\Users\\<username>\\AppData\\Local\\Programs\\Python\\Python38\\python.exe'
Set default Python
Go to This PC → Right-click → Click on Properties → Advanced System Settings.
You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.
You will see a top half for the user variables and the bottom half for System variables.
Check the System Variables and double-click on the Path (to edit the Path).
Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.
Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.