Installing / Updating Python on OS X

Installing / Updating Python on OS X

While Python comes pre-installed on OS X, Apple doesn’t do a good job on keeping the Python runtime environment up to date. Currently, on Mac OS X 10.7.4 “Lion”, entering python -V returns Python 2.7.1. Even worse, Mac OS X 10.6 “Snow Leopard” is still on a Python 2.6 release.
While the latest Python releases are always available on http://www.python.org, updating a Mac isn’t a simple, straight forward process.

Follow along and update your Mac to Python 2.7.3, or 3.3.0 or whatever the newest 2.x and 3.x release might be, when you read this. To update your Mac to something like Python 2.7.3, I assume that

  • your Mac-User account is setup as an “Administrator” account.
  • your Mac already has this folder: /System/Library/Frameworks/Python.framework/Versions/

To read about how to upgrade to Python 3.3, jump to the very bottom of this post.

1. Downloading and Installing the latest Python Release

Go to python.org and pick the most recent Python 2.x release: http://python.org/download.
I picked Python 2.7.3 Mac OS X 64-bit/32-bit x86-64/i386 Installer (for Mac OS X 10.6 and later), an about 18 MB download.
Opening the DMG and executing the installer (Python.mpkg) will install the Python release into /Library/Frameworks/Python.framework, which is not next to the other, already installed Python versions and may lead to some nasty incompatibilities. Moreover, /usr/bin/python still executes python 2.7.1., i.e. some work still needs to be done to really update Python on your Mac.

2. Moving Python into the right place

If Python 2.7.x is already available on the Mac (e.g. on OS X Lion), open a terminal and enter:

sudo rm -R /System/Library/Frameworks/Python.framework/Versions/2.7

This will delete Python 2.7.
In any case, now enter this into the terminal

sudo mv /Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions

which moves the just installed python 2.7.3 release next to the other Python releases.

3. Fixing the Group

Setting group to wheel, just like it’s done for the already installed Python Versions:

sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/2.7

4. Updating the Current Link

sudo rm /System/Library/Frameworks/Python.framework/Versions/Current
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions/Current

5. Fixing /usr/bin

Since python, pythonw, etc. are not linked but actually reside in the /usr/bin directory,
/usr/bin/python still executes python 2.7.1..
Therefore, we are going to remove the old python executable files and replacing them with links into /System/Library/Frameworks/Python.framework/Versions/Current:

5.1. Removing old copies

sudo rm /usr/bin/pydoc
sudo rm /usr/bin/python
sudo rm /usr/bin/pythonw
sudo rm /usr/bin/python-config

5.2. Creating links into /System/…/Python.framework/Versions/Current

sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc /usr/bin/pydoc
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw /usr/bin/pythonw
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python-config /usr/bin/python-config

6. Updating .bash_profile

Use TextMate, pico, or your favorite text editor to edit the hidden ~/.bash_profile file. If you want to be able to execute python tools like idle easily, without providing the path, edit the PATH for Python like this:

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

However, you can also remove those four lines altogether or fall-back to the .bash_profile.pysave file.
Try it by closing the terminal app, re-opening it, and entering python -V as well as /usr/bin/python -V.

7. Updating your IDE

The last remaining step it to tell your IDE about the new Python release that you just installed.
I hope you have discovered PyCharm, JetBrain’s IDE for Python and Django.
In PyCharm, you would open its properties and under “Project Interpreters” change the Python Interpreter, by pointing it to the /System/Library/Frameworks/Python.framework/Versions/2.7 directory.

Updating to Python 3.3

Following the same procedure, you can also update to Python 3.3. E.g, I downloaded the Python 3.3.0 Mac OS X 64-bit/32-bit x86-64/i386 Installer form here: http://python.org/download/

After running the included installer, a script like this (run with sudo) should put everything into the right place and make python 3.3 the default python version.

#!/bin/bash
rm -R /System/Library/Frameworks/Python.framework/Versions/3.3
mv /Library/Frameworks/Python.framework/Versions/3.3 /System/Library/Frameworks/Python.framework/Versions
chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/3.3

rm /System/Library/Frameworks/Python.framework/Versions/Current
ln -s /System/Library/Frameworks/Python.framework/Versions/3.3 /System/Library/Frameworks/Python.framework/Versions/Current

rm /usr/bin/pydoc
rm /usr/bin/python
rm /usr/bin/pythonw
rm /usr/bin/python-config

rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pydoc 
rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python 
rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw 
rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python-config 

ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pydoc3 /usr/bin/pydoc
ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3 /usr/bin/python
ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw3 /usr/bin/pythonw
ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3-config /usr/bin/python-config

78 Comments

  1. Thank you for the upgrade pathway. I’m happily using Python 2.76. When I launch Idle, there is a message The version of Tcl/Tk (8.5.9) in use may be unstable. I’ve “installed” an updated version, but to no avail. The same message appears when launching Idle. Probably need to move something around using Unix. Could you folks help out?

    Thank you

  2. Thanks for detailed explanation, worked like a charm!

  3. I updated to python 3.3.5, python3.3.5 works well, but I can’t start idle.
    here is the error message:

    File “/usr/bin/idle”, line 31
    continue
    ^
    TabError: inconsistent use of tabs and spaces in indentation

    I try to run idle3, but there is another error message:

    -bash: /System/Library/Frameworks/Python.framework/Versions/3.3/bin/idle3: /Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3: bad interpreter: No such file or directory

  4. Everything works great on OSX 10.8.5, including IDLE. On an OSX.9.2 machine, Python installs, is launchable in the Terminal, but can’t get it to open in a separate window. Type in IDLE (after twice installing Active Tcl8.5.15.1.29) and get

    python version 2.7.6 can’t run /usr/bin/idle. Try the alternative(s):
    /usr/bin/idle2.7 (uses python 2.7)

    I’ve removed /usr/bin/idle2.5 and /usr/bin/idle2.6, so there should be no competing versions

    Any help here would be greatly appreciated.

  5. Does anyone here have IDLE working on OSX Mavericks? The instructions above work for installing Python 2.7.6, but IDLE does not work. I can confirm that it does work for 10.8.5. I can find the file for IDLE 2.7.6, but it does not execute in OSX 10.9.2. Can anyone here direct me to a resource that might address this?

    Thank you

  6. Recently upgraded from 2.7.2 to 3.4 – worked like a charm. Thanks for taking the time to do this for us.

  7. You are amazing. Thanks so much.

  8. I followed this guide to update my Mac OS X 10.6.8 Python to 2.7 but when trying to run easy_install I get the following error:
    hemmew7d01:checker-276 rtwmac$ easy_install pip
    python version 2.7.6 can’t run /usr/bin/easy_install. Try the alternative(s):

    /usr/bin/easy_install-2.5 (uses python 2.5)
    /usr/bin/easy_install-2.6 (uses python 2.6)

    Run “man python” for more information about multiple version support in
    Mac OS X.

    I only found a SO post that has the same problem after using this guide, but no solution: http://stackoverflow.com/questions/22334540/how-to-install-packages-o-python

    Any help is appreciated.

  9. Thank you for this! I followed the steps to upgrade to Python 3.3 but have two remaining problems:

    – When I try “which python” I still get 2.6.
    – Since the upgrade, I reinstalled a previously-installed package – Twython – using pip install. Pip told me the package was already installed (requirement already up to date) and shows it installed in the Versions/3.3/lib/python3.3/site-packages folder. Yet my code now throws an ImportError: No module named twython.

    What am I missing?

  10. similar error to posted above using python 3.4 on OS10.6:

    File "/usr/bin/easy_install", line 31
    continue
    ^
    TabError: inconsistent use of tabs and spaces in indentation

  11. I had a problem with my FTP program, Fetch after removing version 2.7 and upgrading to 3.4. It wouldn’t start. Installed 2.7.6 based on first set of instructions and Fetch works again. It was an issue with Carbon (as discovered in Console).

  12. Wolf, I understand completely why you did this, and I followed the instructions here. It’s a real pity Apple still doesn’t have a way to keep the Python runtime environment current.

    Heads up, though. If you do this and follow the instructions above, you will break Google Chrome automatic updates if you happen to use that browser, as many people do.

  13. Thank you very much, it worked like a charm.

  14. Installed Python 2.7.7 using installer provided at python.org, which obviates need for the steps above. Symbolic links and removal of previous 2.7.x version performed during install. Version-appropriate Idle works fine from Terminal. Can’t launch either Python or Idle from dock, but this is not a problem for anyone comfortable with the command line. Can launch the multiple Python versions existing in OS 10.9.3 (2.5, 2.6, 2.7) or installed 3.4 in command line with pythonx.x. Tcl/Tk install without a hitch, as well. I am using BASH. Hope this is useful.

  15. Very clear and workable procedure. Thanks a lot. It helped me clean up the Python version mess on my Mac.

  16. Will this configuration get smashed with an upgrade to a newer version of OSX?

  17. THIS finally got me out of my Python mess! I installed a newer version, but TextMate was continuing to use the older one and Userlib2 was not compatible. This resulted in RESTful API request failing.

    This procedure not only helped me fix my problem BUT also helped me understand root cause.

    Thanks!

  18. For OS X 10.9.3, I simply downloaded and installed Python 2.7.7 from https://www.python.org. After I opened a new shell , the default version of Python was 2.7.7

  19. Thanks. Explained to the point.

  20. Hi,

    I am relatively new to Python.

    I am using OS X 10.9.4. I have upgraded Python 3.4.1. And, when loaded Idle3 i am getting warning message

    WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
    Visit http://www.python.org/download/mac/tcltk/ for current information.

    I have downloaded “ActiveTcl8.6.1.1.297588-macosx10.5-i386-x86_64-threaded.dmg” from http://www.python.org/download/mac/tcltk/ tried installed it but it is not working. Could you please advise if i can get any other alternate option to fix this issue and able to use idle3.

  21. Thanks so much for your simple instructions! I used this to update my terminal to Python 3.4, and the only thing I changed in the scripts was the ‘3.3’ to ‘3.4’

  22. Good One. Thanks.

  23. I just upgrading from 2.7 to 3.4. And it works.
    Thanks Paulus.

  24. I tried to update to my 2.7 version, but it deleted the default python on my mac. therefore I installed python 3.4 and tried to update it but it won’t update , although i can see the folder 3.4 in finder”No such file or directory”. Any help?

  25. Hi:

    It’s not that I want to update Python, but rather Python is using 100%+ of my CPU as per the Activity Monitor and I want it to stop — it’s slowing down everything.

    Any ideas?

    Please respond to my email to my email (tedd@spertling.com).

    Thank you,

    tedd

  26. Can you update to show how to install 3.4.1? Link doesn’t apply anymore when upgrading from 3.3

  27. Hi everybody
    Will this work on osx 10.8.5? I somehow completely messed up default Python installation. can this be done via terminal?

    many thanks

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>