Python is on my machine, I just don't know where, if I type python in terminal it will open Python 2.6.4, this isn't in it's default directory, there surely is a way of finding it's install location from here?
12 Answers
sys
has some useful stuff:
$ python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'c:\\Python26\\python.exe'
>>> sys.exec_prefix
'c:\\Python26'
>>>
>>> print '\n'.join(sys.path)
c:\Python26\lib\site-packages\setuptools-0.6c11-py2.6.egg
c:\Python26\lib\site-packages\nose-1.0.0-py2.6.egg
C:\Windows\system32\python26.zip
c:\Python26\DLLs
c:\Python26\lib
c:\Python26\lib\plat-win
c:\Python26\lib\lib-tk
c:\Python26
c:\Python26\lib\site-packages
c:\Python26\lib\site-packages\win32
c:\Python26\lib\site-packages\win32\lib
c:\Python26\lib\site-packages\Pythonwin
c:\Python26\lib\site-packages\wx-2.8-msw-unicode

- 364,293
- 75
- 561
- 662
-
When I type `sys` in Python it says it is not defined... what is going on there? Thanks. – Spacey Oct 15 '14 at 21:32
-
2
-
13Pythonic! This is an OS agnostic answer and doesn't require access to command line. This worked well for me since I only have access to the Python Interpreter. – Robino Jan 08 '16 at 09:29
-
6Please accept this answer as it is multi-platform and doesn't need access to the shell/cmd! – Hack5 Apr 20 '17 at 13:33
-
This also works better when you have multiple pythons installations and access them through py -2 or py -3 for example, since 'which python' will probably display only one – GuiFGDeo Jul 02 '18 at 16:49
-
In unix (mac os X included) terminal you can do
which python
and it will tell you.
-
4@Ned check out http://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-windows – Foo Bah Jul 21 '11 at 04:06
-
@Foo Bah: yes, thanks. Did you see mine was the accepted answer there? :) – Ned Batchelder Jul 21 '11 at 14:53
-
@Ned no offense but I had actually intended to point to the answer that had the most upvotes (it used CMD primitives :) – Foo Bah Jul 21 '11 at 16:11
-
20-1: Definitely not the best answer here. There are both single line and multiline solutions that work on every answer. – ArtOfWarfare Aug 24 '13 at 21:00
-
10This is also not the best answer because many, many more times often than not, your `python` executable is a symlink. `which python` will, in all probability just point to `/usr/bin` or `/usr/local/bin`, which really isn't helpful. – Jay Sep 02 '15 at 14:56
-
An alternative to looking for Windows equivalents to these commands is to install a bash shell on Windows. The "which python" command works when run in Git Bash on Windows. – Asencion Feb 28 '19 at 20:13
-
"which python" works for me on Windows in Bash but not a command prompt. "where python" works in a command prompt. – Micah B. Oct 02 '19 at 20:47
-
is there any command or method of module to find from python file where the python interpretor is? – Mahdi-Jafaree Jun 23 '20 at 09:01
Platform independent solution in one line is
Python 2:
python -c "import sys; print sys.executable"
Python 3:
python -c "import sys; print(sys.executable)"

- 6,735
- 7
- 49
- 57

- 9,238
- 3
- 38
- 76
-
10Worked great on windows! If you're on python 3 you'll have to change it to `print(sys.executable)` – Crazometer May 19 '16 at 04:55
-
the python 3 version should work on python 2 regardless, as the parentheses are just treated as a token – micsthepick Mar 21 '20 at 00:09
For Windows CMD run: where python
For Windows PowerShell run: Get-Command python

- 1,361
- 11
- 20
-
7
-
-
-
If this also included `which python`/ `which python3` for UNIX/Linux, this would be the best answer. +1 anyways. – Xbox One Jul 28 '22 at 03:27
-
Don't know why this one isn't first in the list. Simple, easy, and does exactly what was asked. Thanks! – dpberry178 Aug 10 '22 at 14:58
Have a look at sys.path
:
>>> import sys
>>> print(sys.path)

- 20,356
- 6
- 40
- 33
-
-
sys.path returns a list of directories. I specifically need the one that is Python\Python310 (or whatever version). How do I rule it out to get the correct one? – bruh Jun 27 '22 at 23:25
On UNIX-like systems, you should be able to type which python
, which will print out the path to python
. The equivalent in Windows Command Prompt is where python
, and Get-Command python
in Windows Powershell.
Another (cross-platform) method is to type this into IDLE or REPL (type python
into your terminal):
import re
re.__file__
Or in one line from your terminal:
python -c "import re; print(re.__file__)"
This will print the path to the re
module, consequently showing you where the python
command points to. You can put any other module that you know is installed, and the path will point to that module, also giving you the path to python
.

- 307
- 2
- 13

- 479
- 2
- 9
-
1
-
3
-
-
7
-
1This is how I know that /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ is the same directory as /usr/bin? – Wooble Jul 20 '11 at 20:02
-
2If you don't have the `re` module, then try it with a module that you **do** have. Anything you're expecting to be in the `lib` subdirectory should work, such as `os` (which is pretty essential). – Karl Knechtel Jul 20 '11 at 21:50
-
1@Karl Yup. I just picked something that came standard and assumed anyone reading it would be able to figure out you could substitute any standard module. My mistake. ;) – tiny_mouse Jul 21 '11 at 07:15
If you are using wiindows OS (I am using windows 10 ) just type
where python
in command prompt ( cmd )
It will show you the directory where you have installed .

- 1,365
- 18
- 19
For Windows Users:
If the python
command is not in your $PATH
environment var.
Open PowerShell and run these commands to find the folder
cd \
ls *ython* -Recurse -Directory
That should tell you where python is installed

- 7,775
- 7
- 59
- 82
-
-
1@misantroop I dare you to find an instance of windows without powershell; unless you're running Windows Server 2003, it will have powershell – Kellen Stuart Nov 18 '18 at 07:38
-
1Not natively on XP and all of the versions derived from it. Installing software to determine where Python is located seems overkill. – misantroop Nov 18 '18 at 08:00
-
@misantroop yes. XP is a version derived from Windows Server 2003. You will be lucky to find a Windows machine that doesn't have powershell. – Kellen Stuart Nov 19 '18 at 16:06
-
- First search for PYTHON IDLE from search bar
Open the IDLE and use below commands.
import sys print(sys.path)
It will give you the path where the python.exe is installed. For eg: C:\Users\\...\python.exe
Add the same path to system environment variable.

- 101
- 8
On Windows, search for "python", then right-click on it and click "Open file location".

- 25,369
- 29
- 96
- 135