Skip to content

Instantly share code, notes, and snippets.

@DuaneNielsen
Last active December 1, 2019 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DuaneNielsen/8700190ec5732fc05e677fbd81683ca2 to your computer and use it in GitHub Desktop.
Save DuaneNielsen/8700190ec5732fc05e677fbd81683ca2 to your computer and use it in GitHub Desktop.
Setting up Jupyter notebook kernels for multiple CUDA versions.
You can also set the variables in your kernel.json file:
My solution is useful if you need the same environment variables every time you start a jupyter kernel, especially if you have multiple sets of environment variables for different tasks.
To create a new ipython kernel with your environment variables, do the following:
Read the documentation at https://jupyter-client.readthedocs.io/en/stable/kernels.html#kernel-specs
Run jupyter kernelspec list to see a list with installed kernels and where the files are stored.
Copy the directory that contains the kernel.json (e.g. named python2) to a new directory (e.g. python2_myENV).
Change the display_name in the new kernel.json file.
Add a env dictionary defining the environment variables.
Your kernel json could look like this (I did not modify anything from the installed kernel.json except display_name and env):
{
"display_name": "Python 2 with environment",
"language": "python",
"argv": [
"/usr/bin/python2",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"env": {"LD_LIBRARY_PATH":""}
}
Use cases and advantages of this approach
In my use-case, I wanted to set the variable LD_LIBRARY_PATH which effects how compiled modules (e.g. written in C) are loaded. Setting this variable using %set_env did not work.
I can have multiple python kernels with different environments.
To change the environment, I only have to switch/ restart the kernel, but I do not have to restart the jupyter instance (useful, if I do not want to loose the variables in another notebook). See -however - https://github.com/jupyter/notebook/issues/2647
jupyter kernelspec list
Available kernels:
python2 /home/duane/PycharmProjects/.venv/imm/share/jupyter/kernels/python2
cd /home/duane/PycharmProjects/.venv/imm/share/jupyter/kernels/python2
vi kernel.json
{
"display_name": "Python 2",
"language": "python",
"argv": [
"python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"env": {"LD_LIBRARY_PATH":"/usr/local/cuda-9.0/lib64"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment