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:
python3
You should see the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11) | |
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export PATH=/Library/Frameworks/Python.framework/Versions/3.3/bin:$PATH |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
import tornado.web | |
class MainHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write("Hello, Python3") | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
if __name__ == "__main__": | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
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