Thursday, 29 January 2015

Python on Mac OS X

At CoderDojo on Saturday, quite a few children turned up with macs. For the Python groups, this meant the mentors spent quite a lot of time trying to fix the various Python installations and getting fairly basic stuff to work.

I love my mac, but the default Python setup is horrible for all but the most basic activities. Over the years I've managed to put together a procedure that fixes the Python environment so that most things (e.g. installing modules with pip) works as reliably as on a normal Linux platform.

There are many other/better ways to achieve the same goal. Using Homebrew works well, but does also involve installing Xcode.  My procedure, which minimises the need to install much else other than the latest Python version. Of course if you're going to use a Mac for any serious development, you'll almost want the Xcode command line tools as a minimum.  But I'm thinking of children who have maybe been loaned a Macbook by their parents, who won't want the SSD filled up with Xcode stuff they're never going to use.

I've tested this on Mountain Lion, Mavericks and Yosemite, but it should work on older versions of OS X.

All of these steps should be preformed using the Terminal.

1. First of all, remove the default Python that comes pre-installed on the Mac.

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

2. Then download the latest version from python.org and run .pkg installer. That will plonk the new version in /Library/Frameworks/Python.framework/Versions/2.7. You can leave it there but I've found that it is better to move it to the same path under /System:

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

3. then set the group permissions:

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

4. Now remove the obsolete simlinks:

sudo rm /System/Library/Frameworks/Python.framework/Versions/Current
sudo rm /usr/bin/pydoc
sudo rm /usr/bin/python
sudo rm /usr/bin/pythonw
sudo rm /usr/bin/python-config
sudo rm /usr/bin/easy-install

5. Then create new ones for the new version you've just installed:

sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/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
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/easy_install /usr/bin/easy_install

6. Now, using your favourite text editor (e.g. nano), modify your  .bash_profile file to set up the correct path. Check carefully because the existing line may look very similar (only /System needs adding normally).  Make sure it has a section that looks 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

7.  Now we'll manually fix some absolute paths in a couple of files. Edit /usr/bin/easy_install
and on the very first line, change the path to include /System in the same way as we did to the path in bash_profile.  Then to ensure that idle works correctly, make an identical change to the same line in System/Library/Frameworks/Python.framework/Versions/2.7/bin/idle. You will need to use sudo with whichever editor you choose to edit these files. 

8. Now you can use easy_install to install pip and readline
sudo easy_install readline
sudo easy_install pip

That should be it! To test everything works, try installing a new Python module (e.g. the dateutil module is really useful);

sudo pi install python-dateutil

If this succeeds with no errors, fire up idle and check that you can run import dateutil successfully via the console.

1 comment:

  1. The python 2.7 docs (https://docs.python.org/2.7/using/mac.html) say:

    "The Apple-provided build of Python is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software"

    Have you encountered any problems with Apple or third-party software that expect a certain version of Python and instead get whatever version you've installed in those system paths?

    ReplyDelete