Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 338
Mikhail Janowski Avatar asked Jul 14 '16 07:07

Mikhail Janowski


People also ask

Can you pip install from Jupyter 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.

Why does Python not install pip?

One of the most common problems with running Python tools like pip is the “not on PATH” error. This means that Python cannot find the tool you're trying to run in your current directory. In most cases, you'll need to navigate to the directory in which the tool is installed before you can run the command to launch it.


2 Answers

! pip install --user <package> 

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

like image 159
Ajinkya Avatar answered Oct 02 '22 09:10

Ajinkya


In IPython (jupyter) 7.3 and later, there is a magic %pip and %conda command that will install into the current kernel (rather than into the instance of Python that launched the notebook).

%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 
like image 42
Eponymous Avatar answered Oct 02 '22 09:10

Eponymous