Skip to content

Instantly share code, notes, and snippets.

@thvitt
Last active March 31, 2023 16:26
Show Gist options
  • Save thvitt/9072336288921f57ec8741eb4b8b024e to your computer and use it in GitHub Desktop.
Save thvitt/9072336288921f57ec8741eb4b8b024e to your computer and use it in GitHub Desktop.
Register a jupyter kernel for the current pyenv.
#!/bin/sh
if [ "$PYENV_VERSION" -ne "" ]
then
name=`pyenv version-name`
python=`pyenv which python`
else
name=`basename "$VIRTUAL_ENV"`
python="$VIRTUALENV/bin/python"
fi
jupyterdir=$(jupyter --data-dir)
kerneldir="${jupyterdir}/kernels/${name}"
echo "Installing jupyter kernel file $name for $python to $kerneldir ..."
pip install ipykernel
mkdir -p "$kerneldir"
cat > "$kerneldir"/kernel.json <<EOF
{
"argv": [ "$python", "-m", "ipykernel", "-f", "{connection_file}" ],
"display_name": "$name",
"language": "python"
}
EOF
cat "$kerneldir"/kernel.json
@aiguofer
Copy link

Hey, thanks for your gist! in case it's useful, I created a pyenv plugin to do this automatically when installing new versions/virtualenvs: https://github.com/aiguofer/pyenv-jupyter-kernel

@RichardBronosky
Copy link

Hey, thanks for your gist! in case it's useful, I created a pyenv plugin to do this automatically when installing new versions/virtualenvs: https://github.com/aiguofer/pyenv-jupyter-kernel

@aiguofer I love your plugin! What a great community!

@telamonian
Copy link

Thanks for this @thvitt! This script works, but when using it I ran into some trouble related to virtual environments and the __PYVENV_LAUNCER__ env var. I added a fork of this gist with a fix here: https://gist.github.com/telamonian/8f3a42cc98dbf08a8e1a8f7c4e8c2588

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment