Setting up a Python environment

(c) 2022 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 document was prepared at Caltech with support financial support from the Donna and Benjamin M. Rosen Bioengineering Center.

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



In this lesson, you will set up a Python computing environment for scientific computing. We will install Anaconda with its associated package manager, conda. It has become the de facto package manager/distribution for scientific use.

Before we get rolling with the Anaconda distribution, we have some considerations and installations to get out of the way first.

macOS users: Install XCode

If you are using macOS, you should install XCode, if you haven't already. It's a large piece of software, taking up about 5GB on your hard drive, so make sure you have enough space. You can install it through the App Store.

After installing it, you need to open the program. Be sure to do that, for example by clicking on the XCode icon in your Applications folder. Upon opening XCode, it may perform more installations. After these are completed, you can close XCode.

Windows users: Install Chrome or Firefox

We will be using JupyterLab in this course. It is browser-based, and Chrome, Firefox, and Safari are supported. Microsoft Edge is not. Therefore, if you are a Windows user, you need to be sure you have either Chrome of Firefox installed.

Uninstalling Anaconda

Unless you have experience with Anaconda and know how to set up environments, if you have previously installed Anaconda with a version of Python other than 3.9, you need to uninstall it, removing it completely from your computer. You can find instructions on how to do that from the official uninstallation documentation.

Downloading and installing Anaconda

Downloading and installing Anaconda is simple.

  1. Go to the Anaconda distribution homepage and download the graphical installer.
  2. Be sure to download Anaconda for Python 3.9 for the appropriate operating system.
  3. Follow the on-screen instructions for installation. When prompted, be sure to "Install for me only."
  4. You may be prompted for optional installations, like PyCharm. You will not need these for the course.

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

Install node.js

node.js is a platform that enables you to run JavaScript outside of the browser. We will not use it directly, but it needs to be installed for some of the more sophisticated JupyterLab functionality. Install node.js by downloading the appropriate installer for your machine here.

Launching JupyterLab and a terminal

After installing the Anaconda distribution, you should be able to launch the Anaconda Navigator. If you are using macOS, this is available in your Applications menu. If you are using Windows, you can do this from the Start menu. Launch Anaconda Navigator.

We will be using JupyterLab throughout the course. You should see an option to launch JupyterLab. When you do that, a new browser window or tab will open with JupyterLab running. Within the JupyterLab window, you will have the option to launch a notebook, a console, a terminal, or a text editor. For the updating and installation of necessary packages, click on Terminal to launch a terminal. You will get a terminal window (probably black) with a bash prompt. We refer to this text interface in the terminal as the command line.

If you prefer, you may use the terminal app of your operating system (PowerShell on Windows, or Terminal on a Mac) instead of JupyterLab's terminal. If you want to launch JupyterLab from the command line, simply type

jupyter lab

on the command line. If your default browser is not supported, you and instead use, e.g.,

jupyter lab --browser=firefox

Launching JupyterLab from the command line

While launching JupyterLab from the Anaconda Navigator is fine, I generally prefer to launch it from the command line on my own machine. If you are on a Mac, open the Terminal program. You can do this hitting Command + space bar and searching for "terminal." Using Windows, you should launch PowerShell. You can do this by hitting Windows + R and typing "powershell" in the text box.

Once you have a terminal or PowerShell window open, you will have a prompt. At the prompt, type

jupyter lab

and you will have an instance of JupyterLab running in your browser. If you want to specify the browser, you can, for example, type

jupyter lab --browser=firefox

on the command line.

It is up to you if you want to launch JupyterLab from the Anaconda Navigator or command line.

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 the packages that came with the Anaconda distribution. To do this, enter the following on the command line:

conda update conda
conda update --all

If anything is out of date, you will be prompted to perform the updates, and press y to continue. (If everything is up to date, you will just see a list of all the installed packages.) 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.

Installations

There are a few installations that you should do to help with the class. First, you will need to allow Bokeh, our primary plotting package, to work with Jupyter notebooks.

conda install -c bokeh jupyter_bokeh

It is also nice to have some convenient color palettes available with the colorcet package.

conda install colorcet

We will use pip to install a few utilities we will make use of.

pip install watermark blackcellmagic jupyterlab-spellchecker iqplot bi1x

You should close your JupyterLab session and terminate Anaconda Navigator after you have completed the build. Relaunch Anaconda Navigator and launch a fresh JupyterLab instance. You are now all set!

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 some of the scientific libraries we will use.

Use the JupyterLab launcher (you can get a new launcher by clicking on the + icon on the left pane of your JupyterLab window) to launch a notebook. In the first cell (the box next to the [ ]: prompt), paste the code below. To run the code, press Shift+Enter while the cursor is active inside the cell. You should see a plot that looks like the one below. If you do, you have a functioning Python environment for scientific computing!

Computing environment