I recently needed to install Python3 on my Mac. While the bearded Linux masses just seem to know this stuff, or it’s already part of their distro, in Mac-Land by default we’re stuck on Python 2.7.2 and guidance is lacking.
So to save people doing the digging I had to do, here’s a quick HOWTO on installing Python3 on your Mac. I like understanding who things work, so this post also details where things are installed.
Install Latest Python 3
Download the lastest Python3 installer (v3.3.0 at time of writing) take care you get the version appropriate for your OSX version:
Follow the on-screen instructions, Python3 should be successfully installed into /Library/Frameworks/Python.framework/Versions/3.3 the installer will also create symlinks for python3 in /usr/local/bin – which makes python3 available from the command line.
Check it works
From the command line type:
You should see the following:
Like all good languages Python has a package manager. Python’s is called pip. Pip can be used to install packages into the Python framework so they can be used in your programs.
Something that is not at all obvious to the uninitiated is that to use pip with Python3, you need to
compile run the various install scripts against Python3, otherwise everything just installs in the Python 2.7 directory (this is the voice of bitter experience speaking).
To work with pip3 we need to first install distribute_setup.py. As far I understand it, distribute_setup.py parses the setup.py script in the python package and ensure everything is compatible with Python3 (correct me if I’m wrong Python community 🙂
Download and run the script as follows, you’ll need to use sudo on mac, as the script will need privs to write into /Library dir
curl -O http://python-distribute.org/distribute_setup.py sudo python3 distribute_setup.py
Now you should be able to install pip, again you’ll need to run the script with python3 (not sure if sudo is definitely required this time)
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py sudo python3 get-pip.py
pip should successfully installed. Now you might be thinking you’re home and dry, however, if you type pip on the command line you’ll probably get Command not found, you’re going to have to alter your Path variable to make pip3 available. So add the following line to your .bash_profile:
Restart you terminal and check if it’s working by doing, the following, and checking that (Python 3.3) is appended at the end.
If you have an older version of pip installed you should still be able to use it by entering pip-2.7
Install Tornado webserver using pip3
So now we can give our new pip a testdrive by installing the Tornado web server. Again on mac we appear to need to use sudo, otherwise strange errors occur:
sudo pip install tornado
You should see tornado being installed successfully into Python3’s site_packages dir /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages
So now we can use Python3 to run our Tornado hello world app, and see it running in the browser
So we can run our hello-world.py using Python3 and we should see it running successfully in the browser at http://localhost:8888