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
Or maybe try “brew install python3” 😉 Visit http://mxcl.github.com/homebrew/ first of course, but no self respecting OS X coder would live without installing a package manager of some flavour 🙂
Thank you!
Awesome ! Thank You
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!
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!
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.
Great, article, Ian!! Just what I was looking for!
Thank you, Thank you, Thank you, Thank you, Thank you, Thank you! You save me.
THANKS!
You sir are a gentleman and a scholar. Thank you!
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.
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.
Just used this. Thanks!
hi there, apparently the “python-distribute” part is deprecated. Here is what I found on other forums : https://pythonhosted.org/setuptools/
if the author is willing to update this very good howto, that would be awesome 🙂
# I Second this…
Get How-to, please update:
” curl -O http://python-distribute.org/distribute_setup.py
sudo python3 distribute_setup.py ”
to:
curl https://bootstrap.pypa.io/ez_setup.py -o – | python3
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!!!!
Great tut and address the hurdles we go through. I’m still cleaning the mess I made! Thanks for sharing!
Thank you. I was able to install pip3 as instructed on my Macbook Pro running Mojave (v 10.14.3). In my case, the binary was installed in usr/local/bin/pip3, so I did not need to modify my bash_profile.