# Running Code at Juypter Kernel Startup

creating a conda kernel environment

I sometimes want to set environmental variables like LD_LIBRARY_PATH and CFLAGS before starting a particular conda kernel in Jupyter, like when I'm using Python code wrapping a C library.

In this case, the important file is the kernel.json. But first, we need to write a file which exports the appropriate environmental variables and then launches the kernel. For example, I put my example of this kind of script in ~/scripts/kernel-intel.sh. It sets up variables for the Intel compiler suite on the cluster:

#!/bin/bash
export MPICC=which mpicc
exec /home/zequnl/.conda/envs/ps/bin/python -m ipykernel_launcher "\$@"

In this case you should replace the last line with whatever executable the command which python tells you, when your conda environment is activated.

Next, run this bash command to find out where your Jupyter kernels are located.

jupyter kernelspec list

For example, I get the output

Available kernels:
julia-1.1    /home/zequnl/.local/share/jupyter/kernels/julia-1.1
julia-1.2    /home/zequnl/.local/share/jupyter/kernels/julia-1.2
julia-1.3    /home/zequnl/.local/share/jupyter/kernels/julia-1.3
ps           /home/zequnl/.local/share/jupyter/kernels/ps
python3      /home/zequnl/.conda/envs/ps/share/jupyter/kernels/python3


The conda environment I want to modify is ps. Navigate to the folder corresponding to that kernel,

cd /home/zequnl/.local/share/jupyter/kernels/ps

Now open up kernel.json, and change it to something like

{
"argv": [
"/home/zequnl/scripts/kernel-intel.sh",
"-f",
"{connection_file}"
],
"display_name": "Python 3 (ps)",
"language": "python"
}

You should modify the path to the script, as well as the display name, for your own configuration.