I'm often working on a python project where I start the project with a virtual environment 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 but use a kernel associated with another virtual environment?
I know I could remove the jupyter related requirements 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.
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).
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"
}
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/