228

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?

1
  • 30
    I just found mine in C:\Users\<user>\AppData\Local\Programs\Python\Python36
    – relipse
    Jul 22, 2016 at 15:51

12 Answers 12

256

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
6
  • When I type sys in Python it says it is not defined... what is going on there? Thanks.
    – Spacey
    Oct 15, 2014 at 21:32
  • 2
    @Learnaholic - You need to import sys first.
    – Tony
    Oct 16, 2014 at 10:10
  • 13
    Pythonic! 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 8, 2016 at 9:29
  • 6
    Please accept this answer as it is multi-platform and doesn't need access to the shell/cmd!
    – Hack5
    Apr 20, 2017 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 2, 2018 at 16:49
188

In unix (mac os X included) terminal you can do

which python

and it will tell you.

8
  • 4
    @Ned check out stackoverflow.com/questions/304319/…
    – Foo Bah
    Jul 21, 2011 at 4:06
  • @Foo Bah: yes, thanks. Did you see mine was the accepted answer there? :) Jul 21, 2011 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, 2011 at 16:11
  • 20
    -1: Definitely not the best answer here. There are both single line and multiline solutions that work on every answer. Aug 24, 2013 at 21:00
  • 10
    This 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 2, 2015 at 14:56
129

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)"
2
  • 10
    Worked great on windows! If you're on python 3 you'll have to change it to print(sys.executable)
    – Crazometer
    May 19, 2016 at 4:55
  • the python 3 version should work on python 2 regardless, as the parentheses are just treated as a token Mar 21, 2020 at 0:09
58

For Windows CMD run: where python

For Windows PowerShell run: Get-Command python

5
  • 7
    This one is really nice 👍 Sep 9, 2019 at 20:36
  • doesn't work in PowerShell for some reason May 21, 2021 at 21:19
  • I updated the answer to include powershell
    – SitiSchu
    Jun 3, 2021 at 23:45
  • If this also included which python/ which python3 for UNIX/Linux, this would be the best answer. +1 anyways.
    – Xbox One
    Jul 28, 2022 at 3: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, 2022 at 14:58
27

Have a look at sys.path:

>>> import sys
>>> print(sys.path)
2
  • this one should be the recc approach on windows Jul 21, 2017 at 20:05
  • 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, 2022 at 23:25
15

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.

7
  • 1
    What if they don't have the re module. Jul 20, 2011 at 19:27
  • 3
    How do you get a Python interpreter without re? :) Jul 20, 2011 at 19:28
  • 7
    And forgot where you put it, did you? ;-)
    – Steven
    Jul 20, 2011 at 20:01
  • 1
    This is how I know that /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ is the same directory as /usr/bin?
    – Wooble
    Jul 20, 2011 at 20:02
  • 2
    If 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). Jul 20, 2011 at 21:50
10

To find all the installations of Python on Windows run this at the command prompt:

dir site.py /s

Make sure you are in the root drive. You will see something like this.

9

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 .

3

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

5
  • Windows only and on top of that requires PS.
    – misantroop
    Nov 10, 2018 at 10:16
  • 1
    @misantroop I dare you to find an instance of windows without powershell; unless you're running Windows Server 2003, it will have powershell Nov 18, 2018 at 7:38
  • 1
    Not natively on XP and all of the versions derived from it. Installing software to determine where Python is located seems overkill.
    – misantroop
    Nov 18, 2018 at 8: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. Nov 19, 2018 at 16:06
  • Thanks @KolobCanyon - this is helpful
    – Ulysses
    Feb 12, 2019 at 4:32
3
  1. First search for PYTHON IDLE from search bar
  2. Open the IDLE and use below commands.

    import sys print(sys.path)

  3. It will give you the path where the python.exe is installed. For eg: C:\Users\\...\python.exe

  4. Add the same path to system environment variable.

0
1

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

0
0

Run below command

where python

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.