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
-
When I type
sys
in Python it says it is not defined... what is going on there? Thanks.– SpaceyOct 15, 2014 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.– RobinoJan 8, 2016 at 9:29
-
6Please accept this answer as it is multi-platform and doesn't need access to the shell/cmd!– Hack5Apr 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– GuiFGDeoJul 2, 2018 at 16:49
In unix (mac os X included) terminal you can do
which python
and it will tell you.
-
4
-
@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 BahJul 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
-
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.– JaySep 2, 2015 at 14:56
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)"
-
10Worked great on windows! If you're on python 3 you'll have to change it to
print(sys.executable)
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
For Windows CMD run: where python
For Windows PowerShell run: Get-Command python
-
7
-
-
-
If this also included
which python
/which python3
for UNIX/Linux, this would be the best answer. +1 anyways.– Xbox OneJul 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! Aug 10, 2022 at 14:58
Have a look at sys.path
:
>>> import sys
>>> print(sys.path)
-
-
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?– bruhJun 27, 2022 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
.
-
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?– WoobleJul 20, 2011 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 thelib
subdirectory should work, such asos
(which is pretty essential). Jul 20, 2011 at 21:50
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.
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 .
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
-
-
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
-
1Not natively on XP and all of the versions derived from it. Installing software to determine where Python is located seems overkill. 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
-
- 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.
On Windows, search for "python", then right-click on it and click "Open file location".