Jupyter Lab Setup with a Python Virtual Environment

Create the Python virtual environment using a Python executable and the venv module along with the path to where the virtual environment should be created:

python -m venv ~/dev/py/env/lab

To learn more about virtual environments, as well as building and using non-system Python executables, see the Python Virtual Environments and Compiling Python from Source Code guides.

Activate the virtual environment:

source ~/dev/py/env/lab/bin/activate

Install Jupyter Lab:

pip install jupyterlab

The IPython kernel will be installed as a dependency of Jupyter Lab.

Add the newly created virtual environment to the kernel specification:

python -m ipykernel install --user --name=lab

Install any desired Jupyter Lab extensions:

pip install jupyterlab-spellchecker jupyterlab-lsp 'python-lsp-server[all]' jupyterlab-vim jupyterlab-code-formatter black isort

Install any desired Python packages:

pip install matplotlib networkx

Navigate to the directory of where you want the new project/notebook to be created:

cd ~/dev/prj/lab

Launch Jupyter Lab:

jupyter lab

On the launcher page, select the newly created virtual environment (lab) to create a notebook that uses that virtual environment.

Recommended configuration changes:

  • Change to dark theme.
  • Increase code and text font size.
  • Enable extensions in the Extension Manager.
  • Enable continuous hinting in the LSP Code Completion sub-menu.
  • Enable autocompletion in the Code Completion sub-menu.
  • Ignore error code E303 in LSP Diagnostics sub-menu.
  • Enable auto closing brackets and quotations in Code Mirror sub-menu.
  • Change the PyCodeStyle max line length to 88 to match the black formatter in the Language Servers sub-menu (pylsp.plugins.pycodestyle.maxLineLength).
  • Enable line numbers in the Notebook sub-menu.
  • Enable code folding in the Notebook sub-menu.
  • Change language of the spell checker to match local language (e.g. en-au).
  • Change to dark theme.