55

I wonder if it's possible to install python packages without leaving the IPython shell.

aculich
  • 14,545
  • 9
  • 64
  • 71
satoru
  • 31,822
  • 31
  • 91
  • 141

7 Answers7

104

See the accepted answer from @Chronial which is the best way to do this in modern ipython or jupyter (as of 2018) is to use the %pip magic:

%pip install my_package

The answer below from 2011 is now outdated: See the accepted answer for an easier way to do this in modern jupyter.

You can use the ! prefix like this:

!pip install packagename

The ! prefix is a short-hand for the %sc command to run a shell command.

You can also use the !! prefix which is a short-hand for the %sx command to execute a shell command and capture its output (saved into the _ variable by default).

aculich
  • 14,545
  • 9
  • 64
  • 71
  • 4
    Actually, you can just prefix the command with an ! e.g. `!pip install packagename`. – Thomas K Dec 30 '11 at 14:09
  • 1
    @ThomasK Yes, you are right! I updated my answer to use `!` and also explain `!!`, `%sc`, and `%sx`. I am going to suggest to the iPython maintainers that they update the `%quickref` docs to clearly mention `!` and `!!` as alternates! – aculich Dec 30 '11 at 17:39
  • 1
    Great, thanks. I've just seen your pull request (I am one of the IPython devs ;) ) – Thomas K Dec 30 '11 at 18:39
  • 1
    @ThomasK Awesome! Glad to see that IPython devs are active over here. – aculich Dec 30 '11 at 19:20
  • 2
    How would you specify the python version? For example, I have Python 2.7 and 3.5 kernels in my notebooks. When I use the `!pip install` the package is only installed for version 3.5. – EntryLevelR Mar 14 '17 at 21:58
  • 1
    @EntryLevelR you are right. The best [answer](https://stackoverflow.com/a/53605684/13394817) is written in another question on SO. – Ali_Sh Sep 08 '21 at 14:38
19

This answer is outdated: See the accepted answer for an easier way to this in modern jupyter.


aculich's answer will not work in all circumstances, for example:

  • If you installed ipython/jupyter in a venv and run it directly via the venv's python binary
  • If you have multiple python versions, like EntryLevelR.

The correct command is:

import sys
!{sys.executable} -m pip install requests
Chronial
  • 66,706
  • 14
  • 93
  • 99
10

The best way to do this in modern ipython or jupyter is to use the %pip magic:

%pip install my_package
Chronial
  • 66,706
  • 14
  • 93
  • 99
  • 3
    Chronial, gave credit to you in [my updated answer](https://stackoverflow.com/a/8675049/462302) which I updated since I have the most upvotes for my old answer so people can quickly get to the newest solution. – aculich Nov 24 '21 at 17:15
9
import pip
pip.main(['install', 'package_name'])

The above shell-based answers don't work unless pip is in your $PATH (e.g. on Windows).

hurfdurf
  • 342
  • 1
  • 3
  • 9
3

I like hurfdurf's answer, but on its own iPython may not recognize the new module (especially if it adds to the library path). Here's an augmented example with iPython 3:

import pip
pip.main(['install','pygame'])
# import pygame at this point can report ImportError: No module named 'pygame'
import site
site.main()
# now with refreshed module path...
import pygame
Daniel
  • 137
  • 1
  • 4
0

In case you are using Conda Package Manager, the following syntax might fit your needs

$ conda install -c conda-forge <targetPackageName>

https://pypi.org/project/earthpy/

Julio Nobre
  • 4,196
  • 3
  • 46
  • 49
0

!pip install packagename or similar codes will not be the true answer if we have various python versions, where the desired python version is not the system default one. Such codes will install packages on the system default python version only. From my opinion, the following code would be helpful:

import pip
if int(pip.__version__.split('.')[0])>9:
    from pip._internal import main
else:
    from pip import main
main(['install', 'package_name'])    # the strings in the square brackets could be specified as needed

This code is written based on pip version, which can be run from within the console and will be applied on any python versions which path is set by the console; This python version specs could be seen by import sys; sys.version, sys.path. I'm not sure this is the best approach, but the aforementioned code worked for my purposes and !pip install packagename or %pip install packagename did not (where python: 2.7.9, pip: 19.2.3, IPython QtConsole: 3.1.0).

Ali_Sh
  • 2,667
  • 3
  • 43
  • 66