BE 150/Bi 250b 2018: Setting up a Python distribution

(c) 2018 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 tutorial, you will set up a Python computing environment for scientific computing. There are two main ways people do this.

  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.

Downloading and installing Anaconda

Downloading and installing Anaconda is simple.

  1. Go to the Anaconda Downloads page.
  2. Download Anaconda for Python 3.6.
  3. Follow the on-screen instructions for installation.

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

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 use conda to install and update packages.

conda works from the command line. To get a command line prompt, do the following.

  • Mac: 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 you can select the Terminal Application.
  • Windows: Fire up PowerShell. To do this, select "Search programs and files" from the Start menu and type "powershell" and hit enter. This works on Windows 7 and presumably also on Windows 8 and 10.
  • Linux: If you're using Linux, it's a good bet you already know how to navigate a terminal.

Now that you have 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 proceed.

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.

Firing up a Jupyter notebook or Jupyter Lab

For most of our computing in thie class, we will use a Jupyter notebook. You can either work directly with classic notebooks in your browser, or use JupyterLab, which is currently in beta, but is much more feature rich, and I will probably use it in most demos in class. Both of these applications runs in a browser, and you can either launch them using the Anaconda Navigator or from the command line.

To launch from the Anaconda Navicator, simply double click on the Anaconda icon and a window with several options will appear. You can either choose to launch JupyterLab (if it is installed, see below for installation) or a Jupyter notebook, which you can do by clicking on the appropriate icon.

To launch a Jupyter notebook from the command line, enter

jupyter notebook

and hit enter. Jupyter will launch in a browser window. To the upper right, you can use a pulldown New menu to create a new Python 3 Jupyter notebook. This will open a new tab or window with a fresh notebook.

To use JupyterLab from the command line, you can first install it by entering

conda install jupyterlab

on the command line and hit enter. You can then launch JupyterLab on the command line using

jupyter lab

The JupyterLab Launcher will give you several options. You should launch a new Python 3 notebook. (The file that JupyterLab saves will be a Jupyter notebook.)

Now, you should launch a Jupyter notebook or Jupyter lab and make a plot to check to make sure your installation works. If it does not, we will troubleshoot it in class.

Now that you have a notebook ready to do, in the first cell, copy and paste the code below. Then press Shift+Enter, and you should see the graphic displayed below. If you do, you have successfully set up your Python environment for the class!

In [1]:
import numpy as np
import bokeh.io
import bokeh.plotting

bokeh.io.output_notebook()

# Generate x values
x = np.linspace(0.0, 10.0, 100)
y = x * np.exp(-x)

# Generate the plot
p = bokeh.plotting.figure(plot_width=500,
                          plot_height=300,
                          x_axis_label='x',
                          y_axis_label='y')
p.line(x, y, line_width=2, line_join='bevel')
bokeh.io.show(p)
Loading BokehJS ...