In your Python interpreter, type the following commands:
>>> import os, sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
Answer from elo80ka on Stack OverflowIn your Python interpreter, type the following commands:
>>> import os, sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
If you have Python in your environment variable then you can use the following command in cmd or powershell:
where python
or for Unix enviroment
which python
command line image :

shell - Is there a Python equivalent to the 'which' command - Stack Overflow
Python not found by command prompt
startup - What command can be used to find where Python is installed? - Unix & Linux Stack Exchange
Python appears in cmd
So I've been having trouble with Python and I want to know which installation I'm using (I have two user profiles on Windows, my old one and the one I made explicitly to code in, and they both have Python installed).
I tried looking it up, but it only led to this madness you see below:
Microsoft Windows [Version 10.0.19043.1348]
(c) Microsoft Corporation. All rights reserved.
C:\Users\willi>!type python
'!type' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\willi>type python
The system cannot find the file specified.
C:\Users\willi>type Python
The system cannot find the file specified.
C:\Users\willi>Python type
Python: can't open file 'C:\Users\willi\type': [Errno 2] No such file or directory
C:\Users\willi>Python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
>>> .quit
File "<stdin>", line 1
.quit
^
SyntaxError: invalid syntax
>>> quit
Use quit() or Ctrl-Z plus Return to exit
>>> quit()
C:\Users\willi>python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> !type Python
File "<stdin>", line 1
!type Python
^
SyntaxError: invalid syntax
>>> type
<class 'type'>
>>> type python
File "<stdin>", line 1
type python
^
SyntaxError: invalid syntax
>>> type Python
File "<stdin>", line 1
type Python
^
SyntaxError: invalid syntax
>>> type(Python)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Python' is not defined
>>>How do I view Python's location in command prompt?
Python 3.3 added shutil.which() to provide a cross-platform means of discovering executables:
http://docs.python.org/3/library/shutil.html#shutil.which
Return the path to an executable which would be run if the given cmd was called. If no cmd would be called, return None.
Sample calls:
>>> shutil.which("python")
'/usr/local/bin/python'
>>> shutil.which("python")
'C:\\Python33\\python.EXE'
Unfortunately, this has not been backported to 2.7.x.
An option for Python 2 and 3:
from distutils.spawn import find_executable
find_executable('python') # '/usr/bin/python'
find_executable('does_not_exist') # None
find_executable(executable, path=None) simply tries to find 'executable' in the directories listed in 'path'. Defaults to os.environ['PATH'] if 'path' is None. Returns the complete path to 'executable' or None if not found.
Keep in mind that unlike which, find_executable does not actually check that the result is marked as executable. You may want to call os.access(path, os.X_OK) to check that on your own if you want to be certain that subprocess.Popen will be able to execute the file.
Also of note, shutil.which of Python 3.3+ has been backported and made available for Python 2.6, 2.7, and 3.x via the 3rd-party module whichcraft.
It is available for installation via the aforementioned GitHub page (i.e. pip install git+https://github.com/pydanny/whichcraft.git) or the Python package index (i.e. pip install whichcraft). It can be used like such:
from whichcraft import which
which('wget') # '/usr/bin/wget'