How to Install Packages from the Jupyter Notebook?

Python package managers, like Anaconda and pip, have made our life much simpler working with Python in different operating systems. However, if you work long enough, you are likely to encounter weird installation problems. One such problem is even if you have installed a package, you won’t be able to import it in the Jupyter notebook. Yes, that can happen 🙂 And it can drive you nuts 🙂

Luckily, you are not alone. And many have faced this issue and Jake VanderPlas has a blogpost late 2017 showing How to Install Packages from the Jupyter Notebook for both Anaconda and pip managers.

It is tempting to try out the executing commands from Jupyter cell, like

# don't try
!conda install --yes numpy

to install numpy from Jupyter cell. That was my first thought too, but Jake explains why this is a bad idea and gave the better solution.

The right way to install a package from Jupyter Notebook that will work in general is

# Install a conda package in the current Jupyter kernel
import sys
!conda install --yes --prefix {sys.prefix} numpy

Check Jake’s blog post for more details and how to install a package with pip from Jupyter Notebook.