Mikhail Janowski
Mikhail Janowski

Reputation: 4719

Installing a pip package from within a Jupyter Notebook not working

When I run !pip install geocoder in Jupyter Notebook I get the same output as running pip install geocoder in the terminal but the geocoder package is not available when I try to import it.

I'm using Ubuntu 14.04, Anaconda 4.0.0 and pip 8.1.2

Installing geocoder:

!pip install geocoder

The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting geocoder
  Downloading geocoder-1.15.1-py2.py3-none-any.whl (195kB)
    100% |████████████████████████████████| 204kB 3.2MB/s 
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): ratelim in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): click in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): decorator in /usr/local/lib/python2.7/dist-packages/decorator-4.0.10-py2.7.egg (from ratelim->geocoder)
Installing collected packages: geocoder
Successfully installed geocoder-1.15.1

Then try to import it:

import geocoder

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-603a981d39f2> in <module>()
----> 1 import geocoder

ImportError: No module named geocoder

I also tried shutting down the notebook and restarting it without any luck.

Edit: I found that using the terminal installs the geocoder package in /home/ubuntu/.local/lib/python2.7/site-packages and using a notebook installs it in /usr/local/lib/python2.7/dist-packages which is not in the path. sys.path.append('/usr/local/lib/python2.7/dist-packages') solves the problem for the current session.

So how can I permanently modify the path or tell pip where to install geocoder?

Upvotes: 130

Views: 398782

Answers (14)

user2138149
user2138149

Reputation: 17448

Figure out which (virtual?) environment your Notebook points to

To be honest, the only way to really understand what you need to do in this context is to figure out which environment your Notebook is currently referencing.

This might be a virtual environment.

I did the following to figure this out:

! which pip
! which pip3

These commands should be run inside a Notebook cell.

They will indiciate the path to your pip/pip3 executable.

Using this information, you can infer which environment your Notebook is pointing to.

For example, my output was something like

/home/username/project_name/some_sub_directory/.venv/bin/pip3

In my particular case, I had 2 existing virtual environments inside my project directory tree.

I then accidentally created a new virtual environment when I created a Jupyter notebook using VS Code.

Install packages

Once you have figured out which environment (maybe virtual environment) that the Jupyter Notebook uses, then you can install packages from the command line in the usual way.

# obviously, go to the correct directory first, then do:
$ .venv/bin/activate
(venv) $ pip3 install jsons

These instructions are for Linux, however the situation for Windows and other OS's is likely to be quite similar.

Upvotes: 0

pplonski
pplonski

Reputation: 5869

There are two ways in which you can install a new package within Jupyter. The first approach is to install package by running pip command in the code cell. The safe command should specify the python executable that is currently used in the kernel:

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

The second way, is to use extension for JupyterLab for managing packages. It is available on Github: https://github.com/mljar/package-manager I'm the co-author of this extension. You can use it to list packages:

list packages in Jupyter

To install a new package in this extension you provide the package name, you can also specify the version, and click Install:

install a new package in Jupyter

You can read more in the article about different ways of package installation in Jupyter.

In Jupyter environment you have Python, virtual environments and kernels - all those terms can be confusing at the beginning, however simple visual interface or using python full path in commands help to avoid problems with packages.

Upvotes: 0

neves
neves

Reputation: 39501

You should never start pip with an exclamation point. It executes a shell command and there's no guarantee that it'll use the python from your running kernel.

Nowadays you can just run pip or conda without any % signal:

pip install pandas

Since automagic is now the default, Jupyter will automatically call %pip and use the correct pip for your kernel.

Remember that it's always better to have your dependencies in an external file called requirements.txt and fix their version number. Than you can just run in your first cell:

pip install -r requirements.txt

The most robust way is really to have a different python kernel for each project. So you don't mix dependencies.

Upvotes: 1

Eponymous
Eponymous

Reputation: 6851

In IPython 7.3 and later, there is a magic %pip and %conda command that will run in the current kernel.

%pip install geocoder

In earlier versions, you need to use sys to fix the problem like in the answer by FlyingZebra1

import sys
!{sys.executable} -m pip install geocoder

Upvotes: 176

FlyingZebra1
FlyingZebra1

Reputation: 1346

%pip install geocoder

in 2019.

In older versions of conda:

import sys
!{sys.executable} -m pip install geocoder

Upvotes: 46

Stuart MacDonald
Stuart MacDonald

Reputation: 19

I had the same problem.

I found these instructions that worked for me.

# Example of installing handcalcs directly from a notebook
!pip install --upgrade-strategy only-if-needed handcalcs

ref: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

Issues may arise when using pip and conda together. When combining conda and pip, it is best to use an isolated conda environment. Only after conda has been used to install as many packages as possible should pip be used to install any remaining software. If modifications are needed to the environment, it is best to create a new environment rather than running conda after pip. When appropriate, conda and pip requirements should be stored in text files.

We recommend that you:

Use pip only after conda

Install as many requirements as possible with conda then use pip.

Pip should be run with --upgrade-strategy only-if-needed (the default).

Do not use pip with the --user argument, avoid all users installs.

Upvotes: 0

Ajinkya
Ajinkya

Reputation: 1129

! pip install --user <package>

The ! tells the notebook to execute the cell as a shell command.

Upvotes: 94

Vinayak Gupta
Vinayak Gupta

Reputation: 57

This worked for me in Jupyter nOtebook /Mac Platform /Python 3 :

import sys
!{sys.executable} -m pip install -r requirements.txt

Upvotes: 4

Dawid Laszuk
Dawid Laszuk

Reputation: 1998

The problem is that pyarrow is saved by pip into dist-packages (in your case /usr/local/lib/python2.7/dist-packages). This path is skipped by Jupyter so pip won't help.

As a solution I suggest adding in the first block

import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')

or whatever is path or python version. In case of Python 3.5 this is

import sys
sys.path.append("/usr/local/lib/python3.5/dist-packages")

Upvotes: 5

robinary
robinary

Reputation: 37

In jupyter notebook under python 3.6, the following line works:

!source activate py36;pip install <...>

Upvotes: 2

Mattony
Mattony

Reputation: 104

Alternative option : you can also create a bash cell in jupyter using bash kernel and then pip install geocoder. That should work

Upvotes: 0

cococ0j
cococ0j

Reputation: 11

conda create -n py27 python=2.7 ipykernel

source activate py27

pip install geocoder

Upvotes: 0

elwarren
elwarren

Reputation: 326

Using pip2 worked for me:

!pip2 install geocoder
...
import geocoder
g = geocoder.google('Mountain View, CA')
g.latlng
[37.3860517, -122.0838511]

Upvotes: -3

casper
casper

Reputation: 19

Try using some shell magic: %%sh %%sh pip install geocoder let me know if it works, thanks

Upvotes: 0

Related Questions