Tidy and Declutter Jupyter Notebooks

Posted on :: 234 Words :: Tags: , ,

Table of Contents

As a general rule, I create a new conda/mamba environment for every new data science project, for simplicity and to avoid compatibility issues. Every 3 months I used to end up with 12 different environments and ipykernels.

This post contains how to create, rename, and remove environments and kernels for tidiness.

A first step to declutter is to take a look at what you already have.

How to see all the environments and kernels installed?

Environments:

conda env list

Also locally at:

  • Miniconda: C:/Users/{windows_username_here}/miniconda3/envs
  • Anaconda: C:/Users/{windows_username_here}/.conda/envs

Kernels:

jupyter kernelspec list

Also locally at:

C:/Users/{windows_username_here}/AppData/Roaming/jupyter/kernels/

How do I create a Jupyter project from scratch?

Environment:

conda create -n {environment_name_here} python={X.X}
# Activate it
conda activate {environment_name_here}

Optional -> consider creating a requirements.txt file (-f) as a best practice.

Kernel:

conda install ipykernel jupyter
python -m ipykernel install --user --name {kernel_name_here}

Your IDE will say something similar to:

Installed kernelspec {kernel_name_here} in

C:/Users/{windows_username_here}/AppData/Roaming/jupyter/kernels/{kernel_name_here}

How to remove them?

Environments:

conda env remove -n {environment_name_here}

Kernels:

jupyter kernelspec uninstall {kernel_name_here}

How to rename them?

Environments:

You can't. One workaround is to create a clone of the environment and then remove the original one (source).

conda env create -n new_name --copy --clone {old_name_here}
conda env remove -n {old_name_here}

Kernels:

The display name for a kernel is found in the kernel.json file in the corresponding directory for the kernel. Edit the display_name property in the kernel.json file and it will change the display name next time you start Jupyter (source).