I can find python version with python --version
But I cannot find the location of python executable. Is there a command like python --path
? If not, is there a reason why?
CodePudding user response:
Use sys.executable
:
python -c 'import sys; print(sys.executable)'
CodePudding user response:
use 'where python' in your terminal to get the path to it
edit
where python
works for windows and which python
works for linux
CodePudding user response:
Use which python
or which python3
.
Work on unix based OS.
For Windows, see other answers.
CodePudding user response:
(Note: the which
and ls -l
commands won't work on Windows, but the general tracking of where Python has been installed on the file system should still be available in sys.base_prefix
)
To elaborate a bit on Wim's answer (I nosed around in the sys namespace before seeing their answer) there are different possible "pythons" you might be interested in: the which python
location. (or in Windows terms, where does it first see python
in a %PATH% location?)
Also you might be interested to know: where is python actually installed on the file system?
sys.base_prefix
helps with the latter.
python -c 'import sys; print(f"{sys.executable=}\n{sys.base_prefix=}" )'
Without an activated virtualenv, I get this on macos using a macport-based python:
sys.executable='/opt/local/bin/python'
sys.base_prefix='/opt/local/Library/Frameworks/Python.framework/Versions/3.10'
And in fact if you dig into what's on in that bin directory, you find:
% ls -l /opt/local/bin/python
lrwxr-xr-x 1 root wheel 25 7 Jan 2022 /opt/local/bin/python -> /opt/local/bin/python3.10
and
% ls -l /opt/local/bin/python3.10
lrwxr-xr-x 1 root admin 75 7 Jun 02:02 /opt/local/bin/python3.10 -> /opt/local/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10
So the actual installed binary is under the location of base_prefix
.
If I activate my virtual environment, base_prefix
stays the same, but the sys.executable
reflects the fact that I am now in a virtualenv
sys.executable='/Users/me/kds2/venvs/bme/bin/python'
sys.base_prefix='/opt/local/Library/Frameworks/Python.framework/Versions/3.10'