188

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?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Kilizo
  • 2,678
  • 4
  • 16
  • 19

11 Answers11

218

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
Ned Batchelder
  • 323,515
  • 67
  • 518
  • 625
  • 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
    @Learnaholic - You need to `import sys` first. – Tony Oct 16 '14 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 08 '16 at 09:29
  • 6
    Please 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
168

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

which python

and it will tell you.

Vega
  • 23,736
  • 20
  • 78
  • 88
dhg
  • 50,861
  • 7
  • 115
  • 141
  • 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
  • 8
    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 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
116

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)"
FistOfFury
  • 5,598
  • 5
  • 43
  • 55
schlamar
  • 8,550
  • 3
  • 35
  • 71
36

On windows running where python should work.

Eric Aya
  • 68,765
  • 33
  • 165
  • 232
SitiSchu
  • 937
  • 9
  • 19
27

Have a look at sys.path:

>>> import sys
>>> print(sys.path)
MRAB
  • 18,864
  • 5
  • 36
  • 31
15

You should be able to type "which python" and it will print out a path to python.

or you can type:

python
>>> import re
>>> re.__file__

and it will print a path to the re module and you'll see where python is that way.

tiny_mouse
  • 479
  • 2
  • 9
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.

Webucator
  • 1,452
  • 13
  • 24
7

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 .

Badri Paudel
  • 489
  • 8
  • 9
2
  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.

Anku g
  • 61
  • 7
1

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

Kolob Canyon
  • 4,727
  • 1
  • 35
  • 59
1

On windows search python,then right click and click on "Open file location".That's how I did