How do I install Python packages in Jupyter notebooks on OpenShift?

Sometimes you want to install a new package that isn’t in your notebook image, usually while you’re prototyping new techniques and aren’t sure if a new package will be useful. Here’s how to install a new package from within the Jupyter notebook itself. Make sure you’re able to launch a Jupyter notebook on OpenShift: follow the quickstart instructions on the Get Started page and then follow the instructions in the How Do I? recipe for launching a Jupyter notebook.

For this example, we’ll install scikit-learn. We’ll start by pasting the following code in to a notebook cell and then executing it by pressing Shift-Enter:

!pip install --user scikit-learn

This will execute the pip install command as the notebook user. Next up, we need to make sure that the user-local library directory is in Python’s search path — paste this code into a notebook cell and execute it: