How to install a Python package from within IPython?

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).


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).


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

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

%pip install my_package