160

I'm a little confused with the python on osx. I do not know if the previous owner of the laptop has installed macpython using macport. And I remembered that osx has an builtin version of python. I tried using type -a python and the result returned

python is /usr/bin/python
python is /usr/local/bin/python

However running both python at these locations give me [GCC 4.2.1 (Apple Inc. build 5646)] on darwin. Do they both refer to the same builtin python mac provided?

I also read that installing macpython one would

     A MacPython 2.5 folder in your Applications folder. In here you
 find IDLE, the development environment that is a standard part of
 official Python distributions...

I looked at Applications, and theres a MacPort folder with python2.6 and the mentioned stuff in it. But running IDLE, i find the same message as above.

Hmm I'm a little confused. Which is which?

1
  • Trivia: Previous owner? If you purchase a computer second hand, just reinstall the OS to clean all previous user files and apps.
    – klys
    Commented May 19, 2023 at 0:55

12 Answers 12

145

This one will solve all your problems not only on Mac but to find it on Linux also ( & every basic shell).

TL;DR (you don't have to go through all the answer - just the 1st half).
LET'S GO

Run in terminal:

which python3

On Mac you should get:

/usr/local/bin/python3

WAIT!!! It's prob a symbolic link, how do you know? Run:

ls -al /usr/local/bin/python3 

and you'll get (if you've installed Python w/ Brew):

/usr/local/bin/python3 -> /usr/local/Cellar/python/3.6.4_4/bin/python3

which means that your

/usr/local/bin/python3 

is actually pointing to (the real location)

/usr/local/Cellar/python/3.6.4_4/bin/python3

That's it!

Longer version (optional): If for some reason, your

/usr/local/bin/python3 

is not pointing to the place you want, which is in our case:

/usr/local/Cellar/python/3.6.4_4/bin/python3

just back it up (+cool trick to add .orig suffix to file):

cp /usr/local/bin/python3{,.orig} 

and run:

rm -rf /usr/local/bin/python3

now create a new symbolic link:

ln -s /usr/local/Cellar/python/3.6.4_4/bin/python3 /usr/local/bin/python3 

and now your

/usr/local/bin/python3

is pointing to

/usr/local/Cellar/python/3.6.4_4/bin/python3 

Check it out by running:

ls -al /usr/local/bin/python3
4
  • 3
    finally.... thank you!!!!! i was running into this issue in a virtual environment. Solutions also works. just need to use the correct python location Commented Feb 22, 2020 at 21:22
  • 2
    This is also a useful reference for me for basic shell itself - lots of incremental steps, and very well explained :)
    – cellepo
    Commented Nov 9, 2020 at 20:16
  • In my case python3 wasn't a symlink and @MaPY's solution showed me the path as - <module 'posixpath' from '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/posixpath.py'> Commented Oct 9, 2023 at 5:26
  • On macOS Ventura/Sonoma Python is located here: /usr/bin/python3 it is a direct binary file, not a symlink.
    – Andrew
    Commented Mar 18 at 18:44
94

I found the easiest way to locate it, you can use

which python

it will show something like this:

/usr/bin/python

2
  • 5
    This does not answer which distribution of Python (who made it). Commented Jan 4, 2019 at 16:11
  • Does it matter at what directory/level you run this command in the terminal? (i.e. macOS iterm2) Commented Aug 14 at 21:48
78

[GCC 4.2.1 (Apple Inc. build 5646)] is the version of GCC that the Python(s) were built with, not the version of Python itself. That information should be on the previous line. For example:

# Apple-supplied Python 2.6 in OS X 10.6
$ /usr/bin/python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

# python.org Python 2.7.2 (also built with newer gcc)
$ /usr/local/bin/python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Items in /usr/bin should always be or link to files supplied by Apple in OS X, unless someone has been ill-advisedly changing things there. To see exactly where the /usr/local/bin/python is linked to:

$ ls -l /usr/local/bin/python
lrwxr-xr-x  1 root  wheel  68 Jul  5 10:05 /usr/local/bin/python@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python

In this case, that is typical for a python.org installed Python instance or it could be one built from source.

64

On Mac OS X, it's in the Python framework in /System/Library/Frameworks/Python.framework/Resources.

Full path is:

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

Btw it's easy to find out where you can find a specific binary: which Python will show you the path of your Python binary (which is probably the same as I posted above).

6
  • yea i knew that. Its just that I'm confused - I ran the interpreter on /usr/local/bin/ and printed sys.executable. It says /usr/bin/python. but I read the docs and it says that the apple-provided was in /usr/bin/ and the macpython's "A symlink to the Python executable is placed in /usr/local/bin/".
    – goh
    Commented Jul 25, 2011 at 17:13
  • Well, it makes sense: when you compile the python-executable yourself and move it to, say /Users/John/Downloads/, it'll show you it's placed at /Users/John/Downloads/python. You can run multiple executable next to each other on the same system. AFAIK the one in Python.framework is the primary one though, so you should rely on that one.
    – cutsoy
    Commented Jul 25, 2011 at 17:17
  • 31
    Actually, the Apple-supplied Pythons in Mac OS X are in /System/Library/Frameworks/Python.framework. Anything in /Library/Frameworks/Python.framework comes from a third-party install, usually a python.org (or other distributor) installer or if you did a default framework build of Python from source.
    – Ned Deily
    Commented Jul 25, 2011 at 19:06
  • 1
    When I run /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 the ps aux output shows me /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python, why is that - I don't see that it's a symlink ? ( Asked this question at AskDifferent )
    – Zitrax
    Commented Apr 2, 2014 at 11:49
  • @Tim Thanks. When I go to /Library/Frameworks/Python.framework/Versions/, I see not 2.7 (which is what I have installed), but an alias file called 'Current', which, when clicked, gives me the error "The operation can't be competed because the original item for 'Current' can't be found."
    – Pyderman
    Commented Jul 22, 2015 at 14:00
9

I checked a few similar discussions and found out the best way to locate all python2/python3 builds is:

which -a python python3
8

On High Sierra

which python

shows the default python but if you downloaded and installed the latest version from python.org you can find it by:

which python3.6

which on my machine shows

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
1
  • This does not answer which distribution of Python (who made it). Commented Jan 4, 2019 at 16:11
5

installed with 'brew install python3', found it here enter image description here

0
4

Run this in your interactive terminal

import os
os.path

It will give you the folder where python is installed

2

run the following code in a .py file:

import sys

print(sys.version)
print(sys.executable)
1

which python3 simply result in a path in which the interpreter settles down.

0

I have a cook recipe for finding things in linux/macos

First update the locate db then do a

locate WHATiWANTtoSEARCH | less

do a /find to find what you are looking for.

to update your locate db in macos do this:

sudo /usr/libexec/locate.updatedb

it sometimes takes a while. Hope this helps :)

0

In my case:

which python3: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
which python3.7: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
which python3.12: /opt/homebrew/bin/python3.12

To change the default python3 command to use Python 3.12 instead of Python 3.7

  1. Create symbolic link

    sudo ln -s /opt/homebrew/bin/python3.12 /usr/local/bin/python3
    
  2. Open the shell configuration file:

    nano ~/.zshrc
    

    Add the following line at the end of the file to set the path for Python 3.12:

    export PATH="/opt/homebrew/bin:$PATH"
    
  3. Source the updated configuration file to apply the changes immediately:

    source ~/.zshrc
    
  4. Check:

    which python3
    python3 --version
    which python3.7
    which python3.12
    

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