Package Management in Python (PIP)

  • You can install, upgrade, and remove packages using a program called pip.

  • By default pip will install packages from the Python Package Index, https://pypi.org

  • Similar to linux repository(YUM, APT), NPM for Node.js, etc

  • Python actually has another, more primitive, package manager called easy_install, which is installed automatically when you install Python itself.

  • pip is vastly superior to easy_install for lots of reasons, and so should generally be used instead.

The Basics

Search Packages

  • You can browse the Python Package Index by going to it in your web browser, or you can use pip’s limited search feature:

pip search django
ERROR: Exception:
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 224, in _main
    status = self.run(options, args)
  File "/opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages/pip/_internal/commands/search.py", line 62, in run
    pypi_hits = self.search(query, options)
  File "/opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages/pip/_internal/commands/search.py", line 82, in search
    hits = pypi.search({'name': query, 'summary': query}, 'or')
  File "/opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/xmlrpc/client.py", line 1109, in __call__
    return self.__send(self.__name, args)
  File "/opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/xmlrpc/client.py", line 1450, in __request
    response = self.__transport.request(
  File "/opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages/pip/_internal/network/xmlrpc.py", line 46, in request
    return self.parse_response(response.raw)
  File "/opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/xmlrpc/client.py", line 1341, in parse_response
    return u.close()
  File "/opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/xmlrpc/client.py", line 655, in close
    raise Fault(**self._stack[0])
xmlrpc.client.Fault: <Fault -32500: "RuntimeError: PyPI's XMLRPC API has been temporarily disabled due to unmanageable load and will be deprecated in the near future. See https://status.python.org/ for more information.">
Note: you may need to restart the kernel to use updated packages.

Install Packages

  • Install the latest version of a package by specifying a package’s name:

pip install requests
Requirement already satisfied: requests in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (2.25.1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (from requests) (1.26.2)
Requirement already satisfied: idna<3,>=2.5 in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (from requests) (2.10)
Requirement already satisfied: chardet<5,>=3.0.2 in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (from requests) (4.0.0)
Requirement already satisfied: certifi>=2017.4.17 in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (from requests) (2020.12.5)
WARNING: You are using pip version 20.3.1; however, version 21.0 is available.
You should consider upgrading via the '/opt/hostedtoolcache/Python/3.8.6/x64/bin/python -m pip install --upgrade pip' command.
Note: you may need to restart the kernel to use updated packages.

Specify version

pip install requests==2.6.0
Collecting requests==2.6.0
  Downloading requests-2.6.0-py2.py3-none-any.whl (469 kB)
?25l
     |▊                               | 10 kB 27.2 MB/s eta 0:00:01
     |█▍                              | 20 kB 33.2 MB/s eta 0:00:01
     |██                              | 30 kB 20.8 MB/s eta 0:00:01
     |██▉                             | 40 kB 19.2 MB/s eta 0:00:01
     |███▌                            | 51 kB 21.8 MB/s eta 0:00:01
     |████▏                           | 61 kB 22.5 MB/s eta 0:00:01
     |████▉                           | 71 kB 20.9 MB/s eta 0:00:01
     |█████▋                          | 81 kB 22.6 MB/s eta 0:00:01
     |██████▎                         | 92 kB 20.5 MB/s eta 0:00:01
     |███████                         | 102 kB 20.5 MB/s eta 0:00:01
     |███████▊                        | 112 kB 20.5 MB/s eta 0:00:01
     |████████▍                       | 122 kB 20.5 MB/s eta 0:00:01
     |█████████                       | 133 kB 20.5 MB/s eta 0:00:01
     |█████████▊                      | 143 kB 20.5 MB/s eta 0:00:01
     |██████████▌                     | 153 kB 20.5 MB/s eta 0:00:01
     |███████████▏                    | 163 kB 20.5 MB/s eta 0:00:01
     |███████████▉                    | 174 kB 20.5 MB/s eta 0:00:01
     |████████████▌                   | 184 kB 20.5 MB/s eta 0:00:01
     |█████████████▎                  | 194 kB 20.5 MB/s eta 0:00:01
     |██████████████                  | 204 kB 20.5 MB/s eta 0:00:01
     |██████████████▋                 | 215 kB 20.5 MB/s eta 0:00:01
     |███████████████▍                | 225 kB 20.5 MB/s eta 0:00:01
     |████████████████                | 235 kB 20.5 MB/s eta 0:00:01
     |████████████████▊               | 245 kB 20.5 MB/s eta 0:00:01
     |█████████████████▍              | 256 kB 20.5 MB/s eta 0:00:01
     |██████████████████▏             | 266 kB 20.5 MB/s eta 0:00:01
     |██████████████████▉             | 276 kB 20.5 MB/s eta 0:00:01
     |███████████████████▌            | 286 kB 20.5 MB/s eta 0:00:01
     |████████████████████▎           | 296 kB 20.5 MB/s eta 0:00:01
     |█████████████████████           | 307 kB 20.5 MB/s eta 0:00:01
     |█████████████████████▋          | 317 kB 20.5 MB/s eta 0:00:01
     |██████████████████████▎         | 327 kB 20.5 MB/s eta 0:00:01
     |███████████████████████         | 337 kB 20.5 MB/s eta 0:00:01
     |███████████████████████▊        | 348 kB 20.5 MB/s eta 0:00:01
     |████████████████████████▍       | 358 kB 20.5 MB/s eta 0:00:01
     |█████████████████████████       | 368 kB 20.5 MB/s eta 0:00:01
     |█████████████████████████▉      | 378 kB 20.5 MB/s eta 0:00:01
     |██████████████████████████▌     | 389 kB 20.5 MB/s eta 0:00:01
     |███████████████████████████▏    | 399 kB 20.5 MB/s eta 0:00:01
     |████████████████████████████    | 409 kB 20.5 MB/s eta 0:00:01
     |████████████████████████████▋   | 419 kB 20.5 MB/s eta 0:00:01
     |█████████████████████████████▎  | 430 kB 20.5 MB/s eta 0:00:01
     |██████████████████████████████  | 440 kB 20.5 MB/s eta 0:00:01
     |██████████████████████████████▊ | 450 kB 20.5 MB/s eta 0:00:01
     |███████████████████████████████▍| 460 kB 20.5 MB/s eta 0:00:01
     |████████████████████████████████| 469 kB 20.5 MB/s 
?25hInstalling collected packages: requests
  Attempting uninstall: requests
    Found existing installation: requests 2.25.1
    Uninstalling requests-2.25.1:
      Successfully uninstalled requests-2.25.1
Successfully installed requests-2.6.0
WARNING: You are using pip version 20.3.1; however, version 21.0 is available.
You should consider upgrading via the '/opt/hostedtoolcache/Python/3.8.6/x64/bin/python -m pip install --upgrade pip' command.
Note: you may need to restart the kernel to use updated packages.

Upgrade Packages

  • You can run pip install --upgrade to upgrade the package to the latest version:

pip install --upgrade requests
Requirement already satisfied: requests in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (2.6.0)
Collecting requests
  Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
Requirement already satisfied: chardet<5,>=3.0.2 in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (from requests) (4.0.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (from requests) (1.26.2)
Requirement already satisfied: certifi>=2017.4.17 in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (from requests) (2020.12.5)
Requirement already satisfied: idna<3,>=2.5 in /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages (from requests) (2.10)
Installing collected packages: requests
  Attempting uninstall: requests
    Found existing installation: requests 2.6.0
    Uninstalling requests-2.6.0:
      Successfully uninstalled requests-2.6.0
Successfully installed requests-2.25.1
WARNING: You are using pip version 20.3.1; however, version 21.0 is available.
You should consider upgrading via the '/opt/hostedtoolcache/Python/3.8.6/x64/bin/python -m pip install --upgrade pip' command.
Note: you may need to restart the kernel to use updated packages.

Remove Packages

  • pip uninstall followed by one or more package names will remove the packages

Display Package information

  • pip show will display information about a particular package:

pip show requests
Name: requests
Version: 2.25.1
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: /opt/hostedtoolcache/Python/3.8.6/x64/lib/python3.8/site-packages
Requires: chardet, idna, urllib3, certifi
Required-by: Sphinx, nbdime
Note: you may need to restart the kernel to use updated packages.

List Packages

  • pip list will display all of the packages installed in the virtual environment

pip list
Package                       Version
----------------------------- ---------
alabaster                     0.7.12
argon2-cffi                   20.1.0
async-generator               1.10
attrs                         20.3.0
Babel                         2.9.0
backcall                      0.2.0
beautifulsoup4                4.9.3
bleach                        3.2.2
certifi                       2020.12.5
cffi                          1.14.4
chardet                       4.0.0
click                         7.1.2
colorama                      0.4.4
cycler                        0.10.0
decorator                     4.4.2
defusedxml                    0.6.0
docutils                      0.16
entrypoints                   0.3
ghp-import                    1.0.0
gitdb                         4.0.5
GitPython                     3.1.12
idna                          2.10
imagesize                     1.2.0
importlib-metadata            3.4.0
ipykernel                     5.4.3
ipython                       7.19.0
ipython-genutils              0.2.0
ipywidgets                    7.6.3
jedi                          0.18.0
Jinja2                        2.11.2
jsonschema                    3.2.0
jupyter-book                  0.9.1
jupyter-cache                 0.4.2
jupyter-client                6.1.11
jupyter-core                  4.7.0
jupyter-sphinx                0.3.1
jupyterlab-widgets            1.0.0
kiwisolver                    1.3.1
latexcodec                    2.0.1
markdown-it-py                0.5.8
MarkupSafe                    1.1.1
matplotlib                    3.3.3
mistune                       0.8.4
myst-nb                       0.10.2
myst-parser                   0.12.10
nbclient                      0.5.1
nbconvert                     5.6.1
nbdime                        2.1.0
nbformat                      5.1.2
nest-asyncio                  1.4.3
nested-lookup                 0.2.21
notebook                      6.2.0
numpy                         1.19.5
oset                          0.1.3
packaging                     20.8
pandocfilters                 1.4.3
parso                         0.8.1
pexpect                       4.8.0
pickleshare                   0.7.5
Pillow                        8.1.0
pip                           20.3.1
prometheus-client             0.9.0
prompt-toolkit                3.0.13
ptyprocess                    0.7.0
pybtex                        0.24.0
pybtex-docutils               1.0.0
pycparser                     2.20
pydata-sphinx-theme           0.4.2
Pygments                      2.7.4
pyparsing                     2.4.7
pyrsistent                    0.17.3
python-dateutil               2.8.1
pytz                          2020.5
PyYAML                        5.4.1
pyzmq                         21.0.1
requests                      2.25.1
Send2Trash                    1.5.0
setuptools                    49.2.1
six                           1.15.0
smmap                         3.0.5
snowballstemmer               2.1.0
soupsieve                     2.1
Sphinx                        3.4.3
sphinx-book-theme             0.0.39
sphinx-comments               0.0.3
sphinx-copybutton             0.3.1
sphinx-panels                 0.5.2
sphinx-thebe                  0.0.8
sphinx-togglebutton           0.2.3
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-bibtex          1.0.0
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        1.0.3
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.4
SQLAlchemy                    1.3.22
terminado                     0.9.2
testpath                      0.4.4
tornado                       6.1
traitlets                     5.0.5
urllib3                       1.26.2
wcwidth                       0.2.5
webencodings                  0.5.1
wheel                         0.36.2
widgetsnbextension            3.5.1
zipp                          3.4.0
WARNING: You are using pip version 20.3.1; however, version 21.0 is available.
You should consider upgrading via the '/opt/hostedtoolcache/Python/3.8.6/x64/bin/python -m pip install --upgrade pip' command.
Note: you may need to restart the kernel to use updated packages.

Freeze Packages

  • pip freeze will produce a similar list of the installed packages, but the output uses the format that pip install expects.

pip freeze
alabaster==0.7.12
argon2-cffi==20.1.0
async-generator==1.10
attrs==20.3.0
Babel==2.9.0
backcall==0.2.0
beautifulsoup4==4.9.3
bleach==3.2.2
certifi==2020.12.5
cffi==1.14.4
chardet==4.0.0
click==7.1.2
colorama==0.4.4
cycler==0.10.0
decorator==4.4.2
defusedxml==0.6.0
docutils==0.16
entrypoints==0.3
ghp-import==1.0.0
gitdb==4.0.5
GitPython==3.1.12
idna==2.10
imagesize==1.2.0
importlib-metadata==3.4.0
ipykernel==5.4.3
ipython==7.19.0
ipython-genutils==0.2.0
ipywidgets==7.6.3
jedi==0.18.0
Jinja2==2.11.2
jsonschema==3.2.0
jupyter-book==0.9.1
jupyter-cache==0.4.2
jupyter-client==6.1.11
jupyter-core==4.7.0
jupyter-sphinx==0.3.1
jupyterlab-widgets==1.0.0
kiwisolver==1.3.1
latexcodec==2.0.1
markdown-it-py==0.5.8
MarkupSafe==1.1.1
matplotlib==3.3.3
mistune==0.8.4
myst-nb==0.10.2
myst-parser==0.12.10
nbclient==0.5.1
nbconvert==5.6.1
nbdime==2.1.0
nbformat==5.1.2
nest-asyncio==1.4.3
nested-lookup==0.2.21
notebook==6.2.0
numpy==1.19.5
oset==0.1.3
packaging==20.8
pandocfilters==1.4.3
parso==0.8.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==8.1.0
prometheus-client==0.9.0
prompt-toolkit==3.0.13
ptyprocess==0.7.0
pybtex==0.24.0
pybtex-docutils==1.0.0
pycparser==2.20
pydata-sphinx-theme==0.4.2
Pygments==2.7.4
pyparsing==2.4.7
pyrsistent==0.17.3
python-dateutil==2.8.1
pytz==2020.5
PyYAML==5.4.1
pyzmq==21.0.1
requests==2.25.1
Send2Trash==1.5.0
six==1.15.0
smmap==3.0.5
snowballstemmer==2.1.0
soupsieve==2.1
Sphinx==3.4.3
sphinx-book-theme==0.0.39
sphinx-comments==0.0.3
sphinx-copybutton==0.3.1
sphinx-panels==0.5.2
sphinx-thebe==0.0.8
sphinx-togglebutton==0.2.3
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-bibtex==1.0.0
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==1.0.3
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.4
SQLAlchemy==1.3.22
terminado==0.9.2
testpath==0.4.4
tornado==6.1
traitlets==5.0.5
urllib3==1.26.2
wcwidth==0.2.5
webencodings==0.5.1
widgetsnbextension==3.5.1
zipp==3.4.0
Note: you may need to restart the kernel to use updated packages.
pip freeze > requirements.txt
Note: you may need to restart the kernel to use updated packages.
f = open('requirements.txt')
print(f.read())
f.close()
alabaster==0.7.12
argon2-cffi==20.1.0
async-generator==1.10
attrs==20.3.0
Babel==2.9.0
backcall==0.2.0
beautifulsoup4==4.9.3
bleach==3.2.2
certifi==2020.12.5
cffi==1.14.4
chardet==4.0.0
click==7.1.2
colorama==0.4.4
cycler==0.10.0
decorator==4.4.2
defusedxml==0.6.0
docutils==0.16
entrypoints==0.3
ghp-import==1.0.0
gitdb==4.0.5
GitPython==3.1.12
idna==2.10
imagesize==1.2.0
importlib-metadata==3.4.0
ipykernel==5.4.3
ipython==7.19.0
ipython-genutils==0.2.0
ipywidgets==7.6.3
jedi==0.18.0
Jinja2==2.11.2
jsonschema==3.2.0
jupyter-book==0.9.1
jupyter-cache==0.4.2
jupyter-client==6.1.11
jupyter-core==4.7.0
jupyter-sphinx==0.3.1
jupyterlab-widgets==1.0.0
kiwisolver==1.3.1
latexcodec==2.0.1
markdown-it-py==0.5.8
MarkupSafe==1.1.1
matplotlib==3.3.3
mistune==0.8.4
myst-nb==0.10.2
myst-parser==0.12.10
nbclient==0.5.1
nbconvert==5.6.1
nbdime==2.1.0
nbformat==5.1.2
nest-asyncio==1.4.3
nested-lookup==0.2.21
notebook==6.2.0
numpy==1.19.5
oset==0.1.3
packaging==20.8
pandocfilters==1.4.3
parso==0.8.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==8.1.0
prometheus-client==0.9.0
prompt-toolkit==3.0.13
ptyprocess==0.7.0
pybtex==0.24.0
pybtex-docutils==1.0.0
pycparser==2.20
pydata-sphinx-theme==0.4.2
Pygments==2.7.4
pyparsing==2.4.7
pyrsistent==0.17.3
python-dateutil==2.8.1
pytz==2020.5
PyYAML==5.4.1
pyzmq==21.0.1
requests==2.25.1
Send2Trash==1.5.0
six==1.15.0
smmap==3.0.5
snowballstemmer==2.1.0
soupsieve==2.1
Sphinx==3.4.3
sphinx-book-theme==0.0.39
sphinx-comments==0.0.3
sphinx-copybutton==0.3.1
sphinx-panels==0.5.2
sphinx-thebe==0.0.8
sphinx-togglebutton==0.2.3
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-bibtex==1.0.0
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==1.0.3
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.4
SQLAlchemy==1.3.22
terminado==0.9.2
testpath==0.4.4
tornado==6.1
traitlets==5.0.5
urllib3==1.26.2
wcwidth==0.2.5
webencodings==0.5.1
widgetsnbextension==3.5.1
zipp==3.4.0
print(f.read())
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-315c20b774c2> in <module>
----> 1 print(f.read())

ValueError: I/O operation on closed file.
pip install requests==2.18.4 --user --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org