Installing Python 3.4

Note: There are many ways to get a stable install of Python onto your system – I personally go with pyenv – but the Anaconda package from Continuum Analytics seems to ably fill all of our needs while being as simplified of a cross-platform installation process as possible.

Currently, the instructions below walk through the installation process for OS X 10.7+, but the steps in Windows should largely be the same. I'll update this page with more instructions if it turns out alternatives to Anaconda are needed.

The Anaconda installation package is 300MB by itself. When installed, Anaconda's Python will take up 1.9GB.

Installing Python 3.4 with the Anaconda installer

Setting up a programming environment can often be a show-stopping obstacle. Luckily for us, Continuum Analytics has created a free, download-and-click installer for Python 3.4 and a bundle of its most useful libraries.

The following steps are for Max OS X 10.7+, though they should be similar for Windows system. The important part is to make sure you're downloading the 3.4 installer (and not the 2.7 version of Python).

Download the Anaconda 3.4 installer

Download Anaconda 3.4. This is what the installation page looks like:

image

The installer package is more than 300MB. After downloading the file, your Downloads folder should have something like:

  Anaconda3-2.2.0-MacOSX-x86_64.pkg

img

Double-click the package file to launch the installer.

img

If possible, choose the Install for me only:

img

Verify the Anaconda installation

If the installation goes smoothly, go to your command-line prompt and type in the following commands:

which python
# /Users/dtown/anaconda/bin/python
python --version
# Python 3.4.3 :: Anaconda 2.2.0 (x86_64)

The respective responses should look similar, but not exactly the same. The most important part is the Python 3.4.3 :: Anaconda 2.2.0 bit.

When I run those commands in my own Terminal, this is what I get:

img

Test out the libraries

With Anaconda successfully installed, you should now have access to the libraries and functions needed to carry out the vast majority of data/statistics/web-related programming tasks for this class.

Try out this simple scraping example, which fetches the page at [www.example.com], extracts the hyperlink, and prints out its href attribute (i.e the URL of the hyperlink):

import requests
import bs4
response = requests.get('http://www.example.com')
soup = bs4.BeautifulSoup(response.text)
print(soup.find('a')['href'])

To run this code, you can do one of two things: Create a Python script and run it from the command-line. Or, execute the code, line-by-line, in iPython.

Create a Python script and run it from the command-line

  1. Create a new text file named example.py
  2. Write the commands given above into example.py
  3. Save the file
  4. From the command-line prompt, run this command:

     python example.py
    

You should see this result:

    http://www.iana.org/domains/example

Here's an animated GIF of the process:

img

Execute code in iPython

iPython is a command-line interface that lets you interactively execute code line-by-line. The end result is the same as writing a script and running it through the python interpreter, as demonstrated above. The difference is that with iPython, you can walk through the code as you execute it line-by-line.

This of course is slower than just putting code in a script file and executing it. But when you don't know yet what the code should be, iPython is a great environment for tinkering with code and seeing how each command works.

Here's an animated GIF of the process. Note that at line 4, I hit the Tab key to do an autocomplete, hence, the weird break in the code:

img

Speaking of the Tab key: taking the 5 to 10 minutes to build the muscle memory needed to reflexively hit Tab to autocomplete commands in iPython is one of the easiest things you can do to increase your short- and long-term success rate at programming.

Check out the official iPython tutorial for more tips and background on how to effectively work with Python.