DEV Community

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

Posted on

2

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay