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?
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With