DEV Community

Cover image for Monitor  for any new conda env and add them as Jupyter kernels
Mostafa Gazar
Mostafa Gazar

Posted on

Monitor for any new conda env and add them as Jupyter kernels

Here is a little script that monitors the conda envs directory on linux and adds a kernel in Jupyter for each new env.

Alternatively you probably should use nb_conda_kernels instead.


#!/bin/bash

# Inspired by: https://unix.stackexchange.com/a/323919
# man page: https://linux.die.net/man/1/inotifywait
inotifywait -m /opt/conda/envs -e create -e moved_to |
    while read path action file; do
      if [[ "$file" == .* ]]; then # Does start with .
        echo "$file ignored"
      else
        # Wait for the env to get created
        echo "Waiting for $path$file/bin/python ..."
        while [ ! -f "$path$file/bin/python" ]; do sleep 1; done

        # Add the new conda env
        echo "Add $file kernel"
        source activate "$file" && \
        pip install --quiet ipykernel && \
        python -m ipykernel install --user --name "$file"
      fi
    done
Enter fullscreen mode Exit fullscreen mode

If you found this helpful spread the word.

Discussion (0)