Installing a pip package from within a Jupyter Notebook not working

213,360

Solution 1

! pip install --user <package>

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

Solution 2

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

Solution 3

%pip install fedex    #fedex = package name

in 2019.

In older versions of conda:

import sys
!{sys.executable} -m pip install fedex     #fedex = package name

*note - you do need to import sys

Solution 4

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

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

Solution 5

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

!source activate py36;pip install <...>
Share:
213,360

Related videos on Youtube

Mikhail Janowski
Author by

Mikhail Janowski

Updated on February 21, 2021

Comments

  • Mikhail Janowski
    Mikhail Janowski about 3 years

    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?

    • Sancao
      Sancao almost 8 years
      Those are Python 2 packages. Is your notebook using a Python 2 kernel or a Python 3 kernel?
    • Mikhail Janowski
      Mikhail Janowski almost 8 years
      Its using Python 2
    • Konstantin
      Konstantin almost 8 years
    • amandeep1991
      amandeep1991 over 5 years
      Check jupyter notebook configuration at C:\Users\<username>\.jupyter\jupyter_notebook_config
    • Murtaza Haji
      Murtaza Haji over 3 years
  • amandeep1991
    amandeep1991 over 5 years
    Question itself mentions use of '!', as per my understanding initially the question was asked to find out why geocoder module is not working even after successful installation.
  • Victor 'Chris' Cabral
    Victor 'Chris' Cabral about 5 years
    This did not work for me. Looks like it is a different python environment based on my experience on SageMaker.
  • waterproof
    waterproof over 4 years
    This doesn't always work, because sometimes the pip that's called with a shell command is not actually the installer for the python version in your virtualenv. Using %pip magic instead of !pip (shell command) is the way to go, as in the answer by @eponymous .
  • Meet
    Meet about 3 years
    Indeed a good solution. Does "install in current kernel" mean that package installed through this method will be available to use while the kernel is running. When it is stopped and run the next time, install command has to be executed again? Correct?
  • Suraj
    Suraj about 2 years
    Worked perfectly for me! Thank you.