Add correct kernel to conda environment
Question:
I am trying to add a new conda environment to be available as a jupyter kernel when I run a Jupyter notebook from my main environment. I’ve developed a hacky solution but I suspect there’s a better way.
I would like to be able to use a kernel from any of my environments from my main environment. To add a new environment, I run the following from my main environment:
python -m ipykernel install --user --name my_new_env
Then, I look at the kernel.json
file that was created and I see this:
(my_main_env) ➜ cat kernel.json
{
"argv": [
"/Users/<username>/opt/anaconda3/envs/my_main_env/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "my_new_env",
"language": "python",
"metadata": {
"debugger": true
}
}
The display name is correct but I would like it to point to my_new_env
, not my_main_env
. I can get it to work by editing this file directly, but that seems like a hack. Is there a better way to do this?
Answers:
You can call specific jupyter binary from any environment, just call them from their own directory.
For example, here I have two envs, one running python 3.8 one running python 3.10, I have installed jupyter in both ending having the following binaries:
/usr/local/Caskroom/mambaforge/base/envs/py38/bin/jupyter
/usr/local/Caskroom/mambaforge/base/envs/py310/bin/jupyter
I can call any of those from my main environment. Each of those will have their kernel running the python
binary in their respective /bin
folder.
Same goes for the kernel.json
I have them in
/usr/local/Caskroom/mambaforge/base/envs/py38/share/jupyter/kernels/python3/kernel.json
/usr/local/Caskroom/mambaforge/base/envs/py310/share/jupyter/kernels/python3/kernel.json
Finally figured it out. The key is to specify which Python is calling ipykernel
. In your case, you want to go into my_main_env
and then run:
/Users/<username>/opt/anaconda3/envs/my_new_env/bin/python -m ipykernel install --user --name my_new_env
I am trying to add a new conda environment to be available as a jupyter kernel when I run a Jupyter notebook from my main environment. I’ve developed a hacky solution but I suspect there’s a better way.
I would like to be able to use a kernel from any of my environments from my main environment. To add a new environment, I run the following from my main environment:
python -m ipykernel install --user --name my_new_env
Then, I look at the kernel.json
file that was created and I see this:
(my_main_env) ➜ cat kernel.json
{
"argv": [
"/Users/<username>/opt/anaconda3/envs/my_main_env/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "my_new_env",
"language": "python",
"metadata": {
"debugger": true
}
}
The display name is correct but I would like it to point to my_new_env
, not my_main_env
. I can get it to work by editing this file directly, but that seems like a hack. Is there a better way to do this?
You can call specific jupyter binary from any environment, just call them from their own directory.
For example, here I have two envs, one running python 3.8 one running python 3.10, I have installed jupyter in both ending having the following binaries:
/usr/local/Caskroom/mambaforge/base/envs/py38/bin/jupyter
/usr/local/Caskroom/mambaforge/base/envs/py310/bin/jupyter
I can call any of those from my main environment. Each of those will have their kernel running the python
binary in their respective /bin
folder.
Same goes for the kernel.json
I have them in
/usr/local/Caskroom/mambaforge/base/envs/py38/share/jupyter/kernels/python3/kernel.json
/usr/local/Caskroom/mambaforge/base/envs/py310/share/jupyter/kernels/python3/kernel.json
Finally figured it out. The key is to specify which Python is calling ipykernel
. In your case, you want to go into my_main_env
and then run:
/Users/<username>/opt/anaconda3/envs/my_new_env/bin/python -m ipykernel install --user --name my_new_env