Lesson 0: Configuring your computer

(c) 2016 Justin Bois. This work is licensed under a Creative Commons Attribution License CC-BY 4.0. All code contained herein is licensed under an MIT license.

This tutorial was generated from a Jupyter notebook. You can download the notebook here.

In this lesson, you will set up a Python computing environment for scientific computing. There are two main ways people set up Python for scientific computing.

  1. By downloading and installing package by package with tools like apt-get, pip, etc.
  2. By downloading and installing a Python distribution that contains binaries of many of the scientific packages needed. The major distributions of these are Anaconda and Enthought Canopy. Both contain IDEs.

In this class, we will use Anaconda, with its associated package manager, conda. It has recently become the de facto package manager/distribution for scientific use.

Python 2 vs Python 3

We are at an interesting point in Python's history. Python is currently in version 3.5 (as of August 24, 2016). The problem is that Python 3.x is not backwards compatible with Python 2.x. Many scientific packages were written in Python 2.x and have been very slow to update to Python 3. However, Python 3 is Python's present and future, so all packages eventually need to work in Python 3. Today, most important scientific packages work in Python 3. All of the packages we will use do, so we will use Python 3 in this course.

For those of you who are already using Anaconda with Python 2, you can create a Python 3 environment.

Downloading and installing Anaconda

Downloading and installing Anaconda is simple.

  1. Go to the Anaconda homepage and download the graphical installer.
  2. You will be prompted for your email address, which you should provide. You may wish to use your Caltech email address because educational users get some of the non-free goodies in Anaconda (like MKL routines that will increase performance).
  3. Be sure to download Anaconda for Python 3.5.
  4. Follow the on-screen instructions for installation.

That's it! After you do that, you will have a functioning Python distribution.

Accessing the command line

Note: Do the steps below only after you have finished the installation of Anaconda.

During the bootcamp, you will need to access the command line. Doing this on a Mac or Linux is simple. If you are using Linux, it's a good bet you already know how to navigate a terminal, so we will not give specific instructions for Linux. For a Mac, you can fire up the Terminal application. It is typically in the /Applications/Utilities folder. Otherwise, hold down Command -space bar and type "terminal" in the search box, and select the Terminal Application.

For Windows, download and install Git Bash. After you have installed it, simply right click anywhere on your Desktop, and you should have an option to run Git Bash (at least that's what happens on Windows 7). You will then have a prompt that looks very much like Mac and Linux users will have.

The conda package manager

conda is a package manager for keeping all of your packages up-to-date. It has plenty of functionality beyond our basic usage in class, which you can learn more about by reading the docs. We will primarily be using conda to install and update packages.

conda works from the command line. Now that you know how to get a command line prompt, you can start using conda. The first thing we'll do is update conda itself. To do this, enter the following on the command line:

conda update conda

If conda is out of date and needs to be updated, you will be prompted to perform the update. Just type y, and the update will proceeed.

Now that conda is updated, we'll use it to see what packages are installed. Type the following on the command line:

conda list

This gives a list of all packages and their versions that are installed. Now, we'll update all packages, so type the following on the command line:

conda update --all

You will be prompted to perform all of the updates. They may even be some downgrades. This happens when there are package conflicts where one package requires an earlier version of another. conda is very smart and figures all of this out for you, so you can almost always say "yes" (or "y") to conda when it prompts you.

Finally, we will use conda to install a package that is not included in the standard Anaconda distribution that we would like to use. This will also verify that conda is working properly on your maching. We will install Seaborn, which is a nice package for data visualization. To do this, type the following on the command line:

conda install seaborn

You will again be prompted to approve the installation. Go for it! Seaborn is pretty cool.

The Atom text editor

Since we will be writing code, we need a text editor to do so. There are countless options. For example, Anaconda comes with an interactive developer environment (IDE) called Spyder, which is built for scientific computing. Sublime Text is a widely used text editor. I have happily used Light Table in the past. For this class, we will use Atom, which is currently my own text editor of choice.

To download and install Atom, go to its website and follow the instructions. Once it's installed, you have a good text editor to use in the bootcamp and beyond.

The default configuration of Atom will work well for you, but you have to be careful with how tabs are defined. In the Atom menu, go to

Packages -> Settings View -> Open

This will open the Settings page for Atom. Scroll toward the bottom of that page, and make sure Tab Length is set to 4. Underneath that, make sure Tab Type is set to soft. As you will soon learn, this is important in Python because indentation matters.

IPython

In the bootcamp, we will be using an IPython shell. IPython is a package that allows for easier interaction with the Python interpreter than the standard interactive Python does. To fire up an IPython shell, just enter

ipython

at the command line. This will give you an interactive IPython shell that we will make great use of, particularly during the beginning of the bootcamp.

Checking your distribution

We'll now run a quick test to make sure things are working properly. We will make a quick plot that requires several of the scientific libraries we will use in the bootcamp.

Open an new file in Atom using

File -> New

With the exception of the obvious omission, paste the code below into the text editor and save it as welcome_bootcamp.py. Make sure you save it in your home directory.

After you do that, type

run welcome_bootcamp.py

at the prompt in the IPython shell. You will get a graphic that looks like the one below. In Windows, you may have to click an icon on the bottom tool bar to view it. If you get this plot, excellent! You now have a functioning Python environment for scientific computing!

In [1]:
# Do not enter the next line.  This is only to prepare this tutorial.
%matplotlib inline

# Paste everything following
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# Generate plotting values
t = np.linspace(0, 2 * np.pi, 200)
x = 16 * np.sin(t)**3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)

# Generate the plot
plt.plot(x, y, 'r-')
plt.text(0, 0, 'bootcamp', fontsize=36, ha='center')
plt.margins(0.02)
plt.xlabel(r'$x$', fontsize=18)
plt.ylabel(r'$y$', fontsize=18)
plt.draw()
plt.show()