Skip to main content

JupyterLab

A development environment for writing Python code including Python notebooks

See: https://github.com/RobinL/cheatsheets_etc/blob/master/jupyter_conda.md

Run notebooks

In Jupyter, before you can successfully run the notebook, you’ll need to select the Jupyter kernel for this project. If it doesn’t appear in the drop-down list, run this in a terminal:

. myproject/venv/bin/activate
python3 -m ipykernel install --user --name="venv" --display-name="My project (Python3)"

Run scripts

And if your project has analytical scripts that run in a terminal you could add:

To run the python scripts, you’ll need to activate the virtual env first:

cd myproject
. venv/bin/activate
python3 myscript.py

Using a virtual environment in Jupyter

It is advisable to use a different virtual environment (venv) for each project you do in Python. There is a little bit of set up to get Jupyter working with a venv. Follow the instructions below to get started:

  1. If you haven’t yet created a virtual environment for your project, in terminal run:

    cd myproject
    python3 -m venv venv
    
  2. In the terminal, inside your project directory, activate your venv:

    source venv/bin/activate
    
  3. Install the module ipykernel within this venv (for creating/managing kernels for ipython which is what Jupyter sits on top of):

    pip install ipykernel
    
  4. Create a Jupyter kernel which is configured to use your venv. (Change the display name to match your project name):

    python3 -m ipykernel install --user --name="venv" --display-name="My project (Python3)"
    
  5. In Jupyter, open your notebook and then select this new kernel by its pretty name in the top right hand corner. It might take a little time/refreshes for it to show up.

To resume work on this after working on another project:

  1. Activate the environment:

    cd myproject
    source venv/bin/activate
    

    Now you’ve activated this terminal with your venv, things you run on the command-line will default to using your venv for python packages, rather than the system’s packages. That’s useful if you run ‘python3’, run python scripts or ‘pip install’ more packages.

  2. Open the notebook - it’s remembered which kernel you wanted to use for this notebook and you can carry on working with the packages available.

Note: Once you have associated the kernel with the venv you dont need to recreate/update it. Any packages that are installed to the venv via pip after the kernel is established are immediately available to the kernel.

Using pipenv in Jupyter

pipenv is another environment manager for Python. In general, please refer to their basic guidance.

Set-up for a project results in the creation of Pipfile and Pipfile.lock in the root directory of your project folder.

The instructions for someone to install the packages specified in Pipefile/Pipefile.lock, are as follows (you don’t create a venv yourself, nor is it necessary to ‘activate’ the pipenv environment):

cd myproject
pipenv install

To use the pipenv in Jupyter, compared to using a venv in Jupyter, the syntax of creating the kernel is simply adjusted to:

pipenv install ipykernel
python3 -m ipykernel install --user --name="pipenv-name" --display-name="My project (Python3)"

And then select the kernel in Jupyter as normal.

This page was last reviewed on 1 May 2022. It needs to be reviewed again on 1 July 2022 by the page owner #analytical-platform .
This page was set to be reviewed before 1 July 2022 by the page owner #analytical-platform. This might mean the content is out of date.