I have spent some time researching and found a way to fix the issue and add multiple kernels to the notebook. I am not sure if this is the best way to do it, however, this could help others facing the issue.
$which python
$/Users/User/opt/anaconda3/bin/python
The above command shows the default interpreter of the system however, Jupyter notebook gets its selected interpreter from a connection file
which it reads during runtime. Its can be found here but the file itself is not important -
from jupyter_client import find_connection_file
find_connection_file()
'/Users/User/Library/Jupyter/runtime/kernel-b1fa5520-5ae5-431a-8768-4ab4b755829f.json'
What is important is the location, since this connection file refers to the available kernel.json
files which actually store the details about the interpreters available to jupyter notebook.
This location is under the same path as - '/Users/User/Library/Jupyter/kernels'
Here you will find a folder for each available interpreter (by default only a single folder). Inside the folder (usually called python3), you will find the kernel.json
In my case, it should have looked like below. However, the path for mine was incorrect. I changed it back to the path to my default system python as below and it fixed my first issue of getting back to the original interpreter -
{
"argv": [
"/Users/User/opt/anaconda3/bin/python", #<------ fixed this
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
Next was adding another interpreter to the jupyter notebook
If you have another interpreter which you want to add to the selection of kernels (Menu > Kernel > Change Kernel), then all you need to do is -
- create a new folder in
'/Users/User/Library/Jupyter/kernels'
- copy the above
kernel.json
inside it
- modify the path and the display name
In my case, I made the following -
{
"argv": [
"/Users/User/tensorflow_macos_venv/bin/python", #<---- changed this
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "tf_mac", #<---- changed this
"language": "python"
}
Once done, you can check your available kernels using -
!jupyter kernelspec list
Available kernels:
python3 /Users/User/Library/Jupyter/kernels/python3
tf_mac /Users/User/Library/Jupyter/kernels/tf_mac
This means that now you can go into the jupyter notebook menu Kernels > Change Kernels and find the above 2 kernels to switch between on the fly.
That's how I solved the above issue. Open to better methods because I am sure there would be.