Unable to reliably use single JypyterLab server with multiple conda environments

Thanks for the link and for clarifying that. Figured sharing what I’d gotten to work was at least worth a shot. Looking at the code for nb_conda_kernels, the amount of custom logic in there and also the fact that it’s in written in python both make me a little uneasy, and I’m not surprised there are issues with process inheritence. I think figuring out what command you’d run a shell and sticking those in a bash (or sh, zsh, etc) script will be your best bet. I also wonder if there’s something specific to Windows going on. These things often seem to work a bit differently between Windows and unix-like systems. It’d be interesting to see if it works any better for you on Windows subsystem for linux - might not be worth the hassle though.

In case it helps, I was able to get a kernel working with a bash script for conda containing just these two lines

conda activate "$1"
python -m ipykernel_launcher -f "$2"

preceded by the result of conda init. In my case, copying and pasting what that command appended to my ~/.bashrc with those lines resulted in:

#!/bin/bash
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/opt/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/opt/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
conda activate "$1"
python -m ipykernel_launcher -f "$2"

Then I called this script from kernel.json with the env-name and {connection_file} as I mentioned in my last answer.

I also got it working by just putting everything in the kernel.json file. I first hardcoded env-nameand {connection_file} in place of "$1" and "$2". Then to get the script in json format, I just pasted the script into a multiline string in a python shell, storing it in a variable, a and ran:

import json
print(json.dumps(a))

and finally passed the result to bash in kernel.json:

{
 "argv": [
  "bash",
  "-c",
  "# >>> conda initialize >>>\n# !! Contents within this block are managed by 'conda init' !!\n__conda_setup=\"$('/opt/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)\"\nif [ $? -eq 0 ]; then\n    eval \"$__conda_setup\"\nelse\n    if [ -f \"/opt/miniconda3/etc/profile.d/conda.sh\" ]; then\n        . \"/opt/miniconda3/etc/profile.d/conda.sh\"\n    else\n        export PATH=\"/opt/miniconda3/bin:$PATH\"\n    fi\nfi\nunset __conda_setup\n# <<< conda initialize <<<\nconda activate kernelenv\npython -m ipykernel_launcher -f {connection_file}"
 ],
 "display_name": "Python (kernelenv)",
 "language": "python"
}

If that doesn’t work, I hope you find something that does. Kernel interrupt is kind of a nice function to have!