Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to install Jupyter notebook in every virtual environment?

I isolate my data science projects into virtual environments using pipenv. However, running a Jupyter notebok does not access the local environment and uses the default IPyKernel. I've seen that you can register virtual environments from within the environment, but this requires installing the ipykernel package which itself requires Jupyter!

Is there anyway to avoid this and just use a single Jupyter install for all virtual environments?

like image 270
user126350 Avatar asked Sep 29 '18 07:09

user126350


People also ask

Do I need to install Jupyter notebook for each environment?

Install Jupyter Notebook / Lab in the base environmentJupyter Notebook can easily be installed using conda. Our plan is to only install it in the base environment, and then just switch between sub-environments to avoid setting up Jupyter Lab in each environment.

How do you run a Jupyter notebook in another environment?

To use your new environment with Jupyter Notebooks, open the Notebook application. Click the New button to open a new notebook. In the drop-down menu under Notebooks, the environment you just created is displayed. To activate that environment, select it.

Should I install Python in virtual environment?

It is always recommended to use a virtual environment while developing Python applications. To create a virtual environment, go to your project's directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands.


2 Answers

I found that there are few problems when reinstall jupyter for each environment separately: i.e. pip install jupyter jupyterlab in new environments.

I had multiple issues (with and without Conda), where Jupyter would install packages to a different python environment when you use !pip install a_package_name within a cell. The shell environment still kept track of the non-environment python, and you can tell this by comparing the outputs of !which python and

import sys
sys.executable

Therefore, when you tried to import the package, it would not be available, because the cells used the environment python/ kernel (as it detected the venv directory).

like image 129
Ben Butterworth Avatar answered Sep 20 '22 18:09

Ben Butterworth


Generally, you'd install jupyter once and do the following in your virtual environments:

pip install ipykernel  
python -m ipykernel install --user

This isn't enough when you're running multiple Python versions.
There's a guide here that tries to address this:
https://medium.com/@henriquebastos/the-definitive-guide-to-setup-my-python-workspace-628d68552e14

It's not 100% failsafe, but it can help you avoid reinstalling jupyter notebook all the time.

like image 36
Hendrik D Avatar answered Sep 19 '22 18:09

Hendrik D