pythonjupyter-notebookanacondajupyter-kernel

Add correct kernel to conda environment


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?


Solution

  • 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