9

I quite often find that this would be useful but I'm not sure there's any way to do this. I'm often working on a python project where I start the project with a virtual environment for the project and a Jupyter notebook. I start adding libraries to the virtual environment as I experiment in the Jupyter notebook. The problem is that if I run

pip freeze > requirements.txt

at the end of my project that file will include the libraries for jupyter in my virtual environment. Is there some way I can run a Jupyter notebook (e.g. in my base conda environment) but use a kernel associated with another virtual environment? This seems like the nicest solutions but I'm not sure if this is possible? I know I could probably do something to remove these from what is frozen but that seems like a hack. I can't see any way to avoid installing at least ipykernel in the target virtual environment

soundofsilence
  • 330
  • 2
  • 12

1 Answers1

2
  1. Make a new directory under the Jupyter kernels directory. If you do not know where to find it, please check here (https://jupyter-client.readthedocs.io/en/stable/kernels.html).

  2. Create a kernel.json file with the following:

{
 "argv": [ "/path-to-env/myenv/bin/python", "-m", "ipykernel",
          "-f", "{connection_file}"],
 "display_name": "myenv",
 "language": "python"
}
  1. That's it! Now you can run jupyter notebook and be able to see a kernel that uses your virtual environment.

Here's a blog that explains it in more detail: https://www.alfredo.motta.name/create-isolated-jupyter-ipython-kernels-with-pyenv-and-virtualenv/

Jensun Ravichandran
  • 959
  • 1
  • 11
  • 28