For Anyone Using Jupyter Notebook — Installing Packages

Installing packages globally and locally

Shivangi Sareen
2 min readMar 1, 2020

Jupyter Notebook is a more interactive and easier-to-use version of the Python shell. If we want to install packages from Jupyter Notebook itself, we can put an exclamation point (!) before the pip3/conda install command. This command will then act as if it were executed in the terminal.

Note: we cannot run pip install from the Python shell. The python shell is not a command line; we type Python code into it, not commands.

Proceed with caution when installing packages in Jupyter notebook. After spending hour after frustrating hour of trying to import a certain package that I pip3/conda installed, here’s the right way to go about installing packages.

There are two different scenarios when installing packages:

Locally installing packages (when using virtual environments):

Set up your virtual environment like this. Once set up, activate the virtual environment. Using terminal:

cd <VIRTUAL-ENV-NAME> 
source bin/activate

So, you’re in your virtual environment directory and the virtualenv is now activated. Type in jupyter notebook in the terminal.

Open a new notebook and import sys. The important step is to add the path of your virtual environment site packages to the system path.

sys.path.append('./lib/python3.7/site-packages')

The ./ is a relative path and assumes that you are currently in your virtual environment directory. A quick way to check in Jupyter notebook is executing !pwd.

After this, you can import any package that you’ve installed with ease.

Globally installing packages:

When you want to install a package that is not for a particular project, rather a package that will be used across directories, the following steps discuss the correct convention.

Here’s what we usually do:

!pip3 install <package_name>

DON’T DO THIS.

Here is how it should actually be done:

import sys
!{sys.executable} -m pip install <package_name>

Or using conda:

import sys
!conda install --yes --prefix {sys.prefix} <package_name>

Going the longer route rather than plain Python ensures that commands are run in the Python installation matching the currently running notebook. So, pip installs the package in the currently-running Jupyter kernel.

This is done to overcome the disconnectedness between Jupyter kernels and Jupyter’s Shell, i.e., the installer points to a different Python version than the one being used in the notebook.

Check out the explanation given here to understand more on why such installation practices must be followed even though the so-called usual way of installing works, mostly (it won’t always work like that).

--

--

Shivangi Sareen

Software Engineer @Apple | Reader | Writer | The Sustainable Edit