loading...
Cover image for Monitor  for any new conda env and add them as Jupyter kernels

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

mgazar_ profile image Mostafa Gazar ・1 min read

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

If you found this helpful spread the word.

Discussion

pic
Editor guide