HOWTO Install Python3, pip3 & Tornado on Mac

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:

Download Python3 here

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:


python3

You should see the following:

Install pip3

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.


pip --version

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

Good luck

Advertisements

17 thoughts on “HOWTO Install Python3, pip3 & Tornado on Mac

  1. I had your (bitter) experience of installing all packages into my 2.7 folder. Thanks to your help, I’ve been able to install pip into my 3.3 framework folder. However, when I call pip it still retrieves the 2.7 package. I’m a novice – I don’t know about all this .bash_profile stuff – and when I tried what you recommended nothing changed. Now I’m nervous because I have this extra $PATH line in my .bash_profile. Help!

    1. I solved my own problem: I deleted the pip 2.7 files from my Python library and then I restarted my terminal. Voila! Do I need to keep the .bash_profile edit?

      Thank you so much!

      1. Glad post helped you, at least a bit 🙂 The bash_profile path is to automatically use the pip3, so if you’ve removed all of python2.7 you should be safe to remove the Path line from your profile.

  2. not a single helpful answers on stackoverflow–a lot of those don’t engage the question, others are just wrong.

    followed your post, worked straight away, thanks very much indeed.

  3. I love you. Thank you.

    I had to change one thing (as of Feb 2014) — “export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/3.3/bin”

    and then the pip worked beautifully. So much better than reading the 200th post where some guru is like, “just type sudo pip install” from the mountaintop.

  4. I love you from 2015, you saved my life. I had that bitter pip experience(performed 3 clean installations because the ‘experts’ keep making the same mistake!) Thanks sooooo much!!!!

  5. Great tut and address the hurdles we go through. I’m still cleaning the mess I made! Thanks for sharing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s