0

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?

rnorris
  • 1,571
  • 2
  • 17
  • 23
jss367
  • 4,759
  • 14
  • 54
  • 76
  • you should first activate the env, then start jupyter notebook, or, you can run directly the jupyter binary you are interested in. – alec_djinn Oct 09 '22 at 16:35
  • If I'm understanding correctly, this wouldn't make the new environment available as a kernel from my main environment though, right? That's my goal here. – jss367 Oct 10 '22 at 05:41

2 Answers2

0

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
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
0

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

jss367
  • 4,759
  • 14
  • 54
  • 76