(Home)

Jupyter installation instructions

Note that for most use cases a basic python installation will do, and you can work through the lab exercises using the basic python interpreter and a text editor. However, if you want to use the interactive version of the course material then you'll need ipython and Jupyter. They also give a lot of useful functionality for interactive work over the basic python interpreter, so are generally nice to have.

Pick the relevant instructions below. After following them, check that Jupyter is installed correctly by opening a terminal and executing:

jupyter --version

If you get an error message along the lines of "command not found" then close the terminal, open a new one, and try again. If it still doesn't work or if you encounter any issues along the way then let me know!

Via Anaconda

This is the best way for Windows. It also works for MacOSX and Linux, though using other package managers is generally better for them.

Just visit the Anaconda download page, download the relevant installer, and follow the installation instructions.

Anaconda comes with everything you need, so you shouldn't need to do anything else.

Via Homebrew (for MacOSX)

Homebrew is a package manager for MacOSX (which doesn't have a package manager by default). The advantage of using Homebrew over Anaconda is that you have more control over what's installed. You can also install various other packages and Homebrew will take care of dependencies and make sure the versions installed are compatible.

Firstly, if you don't already have Homebrew, install it by opening a terminal and executing:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then update Homebrew with:

brew update

Jupyter can be installed with:

brew install jupyterlab

This also installs python and ipython.

To pick up the correct executables on the commandline you need to add:

export PATH="/usr/local/opt/python/libexec/bin:~/Library/Python/3.9/bin:/usr/local/share/python:$PATH"

to your login script, normally ~/.bash_profile. Note that the "3.9" might change depending on exactly which python version you have installed. Then execute your login script with

. ~/.bash_profile

You'll only need to do this the first time, in your current terminal, as ~/.bash_profile is executed any time you open a new terminal.

Then you're good to go!

If you're using a shell other than bash (default on most machines) then you'll need to change the name of the login script accordingly, eg. use ~/.zshrc if you're using zsh. You can check which shell you're using with:

echo $SHELL

Via apt-get (Debian, Ubuntu, etc)

For linux machines, it's generally better to use the package manager, so that everything's installed in a consistent manner. Following this, first update the package manager by opening a terminal and executing:

sudo apt-get update

Then pick up the latest python by installing Python 3, Python Pip, and Python Development with:

sudo apt-get -y install python3 python3-pip python3-dev

Then install ipython and ipython notebook with:

sudo apt-get -y install ipython3

Then follow the instructions to install via pip below.

Via pip (anything with an existing python and pip installation)

If you've already got a basic python installation which includes pip (the python package manager) and you're happy with that version then you can just use pip to install Jupyter.

Depending on your installation, you might need to use pip3 rather than pip in order to use python 3.

First, upgrade pip with:

pip install --upgrade pip

On linux machines, you'll need to use:

sudo -H pip install --update pip

This is because packages are installed system wide, which requires root permissions.

Then simply install Jupyter notebook with:

pip install notebook

or for linux:

sudo -H pip install notebook

Alternatively, you can install only for the current user with

pip install --user notebook

or you can install in a virtual environment, as detailed next.

That's all there is to it!

Installing in a virtual environment

A virtual environment is entirely self contained. It can be useful to install python packages in a virtual environment so that it doesn't interfere with (or break) your system python installation, or if you don't have admin access on your machine.

Following this, you can create a new virtual environment called "env" with

python3 -m venv env

Then you can launch the environment, for OSX/Linux, with

source env/bin/activate

or Windows, with

.\env\Scripts\activate

Then, with the environment active, follow the instructions above for installing via pip. Everything you install with the environment active will only be available in the environment.

When you're done, quit the environment with

deactivate

success

Running the interactive course material

See here for basic instructions on running a Jupyter notebook, once you have Jupyter installed.

All you need to do is open a terminal and execute:

jupyter notebook

This will open the notebook dashboard in your web browser. You can then navigate to and click on any existing .ipynb notebook file to open it, or create a new notebook from the "new" dropdown menu at the top right.

To get the course material, right click here and select "Save link as" to get the lecture material, and do the same here for the lab exercises. You can then open them from the notebook dashboard.

To evaluate a cell in the notebook use Shift+Enter. For more info, select "User Interface Tour" from the "Help" menu at the top of the notebook.

Adding packages to your installation

3rd party packages

You can use pip to install any package from pypi.org, the Python Package Index, and other package indices. See the detailed instructions here. In the majority of cases it's as easy as executing in a terminal:

pip install packagename

Eg, to install the very useful uncertainties package, do

pip install uncertainties

Then next time you run python you'll be able to do:

and use its functionality.

To update an already installed package, just do

pip install --upgrade packagename

User defined packages/modules

When you do an import python searches the directories listed in the environment variable PYTHONPATH, as well as the current working directory, for a package or module of the given name. If you're not familiar with environment variables then see here. So to "install" a package or module you just need to add the directory that contains it to the PYTHONPATH environment variable.

For bash type shells, you can do:

export PYTHONPATH="/path/to/package/:$PYTHONPATH"

If you want this to be done every time you log in then you can add it to your login script, normally ~/.bashrc.

For Windows, you can execute in the terminal:

SET PYTHONPATH="/path/to/package;%PYTHONPATH%"

or to set the value permanently, you can follow the instructions here.

After modifying PYTHONPATH appropriately you'll be able to import your package/module next time you run python.

(Home)