Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Installing and Using Jupyter Notebooks

Open in Colab

You should already have Anaconda Python installed on your system.

Jupyter

Jupyter (Julia, Python, R) notebooks mix code with markdown (a language for basic text formatting) in your browser. This document is a Jupyter notebook, and the text is written in markdown.

You can run code in code cells, like this:

2

We’ll use Jupyter notebooks for Python, but you can use notebooks for other languages as well (such as Julia and R, but also a variety of others).

Install Jupyter to the conda base environment

This will allow you to launch Jupyter notebooks without activating your pycourse environment.

First, launch a terminal, and install notebooks in your base environment

(base) $ conda install -c conda-forge notebook

(-c conda-forge tells conda to install from the conda-forge channel).

Test your installation

After installation, you should see a jupyter command is now available.

(base) $ which jupyter
~/miniconda3/bin/jupyter

Install A Kernel for your environment

The next thing you need to do is install a kernel for your pycourse environment. This will allow you to run code in Jupyter notebooks using the same setup as you would if you activate pycourse.

First, activate your pycourse environment in a terminal

(base) $ conda activate pycourse
(pycourse) $

Next, install the ipykernel package using conda

(pycourse) $ conda install ipykernel -c conda-forge

Now, you run the installation

(pycourse) $ python -m ipykernel install --user --name pycourse --display-name "Python (pycourse)"

Finally, deactivate your pycourse environment

(pycourse) $ conda deactivate

Launch a Jupyter notebook server

Now, to launch a Jupyter notebook server, simply type

(base) $ jupyter notebook

You can launch the notebook server from any directory.

You can either create new notebooks, or launch existing notebooks.

Run notebooks in Google Colab

If you don’t want to install anything, you can run any notebook in this book on Google Colab - all you need is a browser and a Google account. Every notebook has an Open in Colab badge under its title which opens a copy of that notebook on a Colab virtual machine, e.g.

Open in Colab

A few things to know:

  • Colab gives you a temporary machine. Edits and results are lost when the session ends, so use File -> Save a copy in Drive to keep your work.

  • Most of the packages used in this book (numpy, scipy, matplotlib, pandas, scikit-learn, sympy, networkx, numba, torch, ...) are preinstalled. Anything else can be installed from a cell with %pip install <package>.

  • Package versions are whatever Colab currently ships, which will not match the pycourse environment exactly, so numerical output can differ in the last digits.

  • Notebooks that read files from this repository fetch them from GitHub when they detect that they are running on Colab.