Python - PIP



Python's standard library is a large collection of ready-to-use modules and packages. In addition to these packages, a Python programmer often needs to use certain third-party libraries. Third-party Python packages are hosted on a repository called Python Package Index (https://pypi.org/).

To install a package from this repository, you need a package manager tool. PIP is one of the most popular package managers.

The PIP utility is automatically installed with Python's standard distribution especially with version 3.4 onwards. It is present in the scripts folder inside Python's installation directory. For example, when Python 3.11 is installed on a Windows computer, you can find pip3.exe in C:\Python311\Scripts folder.

If pip is not installed by default, it can be installed by the following procedure.

Download get-pip.py script from following URL −

https://bootstrap.pypa.io/get-pip.py

To install run above script from command prompt −

c:\Python311>python get-pip.py

In scripts folder both pip and pip3 are present. If pip is used to install a certain package, its Python 2.x compatible version will be installed. Hence to install Python 3 compatible version, use pip3.

Install a Package

To install a certain package from PyPi, use install command with PIP. Following command installs Flask library in the current Python installation.

pip3 install flask

The package, along with its dependencies if any, will be installed from the PyPI repository. The above command produces following log in the terminal −

Collecting flask
   Downloading Flask-2.2.3-py3-none-any.whl (101 kB)
      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
101.8/101.8 kB 3.0 MB/s eta 0:00:00
Collecting Werkzeug>=2.2.2
   Downloading Werkzeug-2.2.3-py3-none-any.whl (233 kB)
      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
233.6/233.6 kB 7.2 MB/s eta 0:00:00
Collecting Jinja2>=3.0
   Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
133.1/133.1 kB 8.2 MB/s eta 0:00:00
Collecting itsdangerous>=2.0
   Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting click>=8.0
   Downloading click-8.1.3-py3-none-any.whl (96 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
96.6/96.6 kB 5.4 MB/s eta 0:00:00
Requirement already satisfied: colorama in
c:\users\mlath\appdata\roaming\python\python311\site-packages (from
click>=8.0->flask) (0.4.6)
Collecting MarkupSafe>=2.0
   Downloading MarkupSafe-2.1.2-cp311-cp311-win_amd64.whl (16 kB)
Installing collected packages: MarkupSafe, itsdangerous, click,
Werkzeug, Jinja2, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.2 Werkzeug-2.2.3
click-8.1.3 flask-2.2.3 itsdangerous-2.1.2

By default, the latest available version of the desired package is installed. To specify the version required,

pip3 install flask==2.0.0

To test if the package installation is complete, open Python interpreter and try to import it and check the version. If the package hasn't been successfully installed, you get a ModuleNotFoundError.

>>> import flask
>>> print (flask.__version__)
2.2.3
>>> import dummypackage
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dummypackage'

PIP utility works with −

  • PyPI (and other indexes) using requirement specifiers.

  • VCS project urls.

  • Local project directories.

  • Local or remote source archives.

Use requirements.txt

You can perform package installation at once by mentioning the list of required packages in a text file named as requirements.txt.

For example, the following requirements.txt file contains list of dependencies to be installed for FastAPI library.

anyio==3.6.2
click==8.1.3
colorama==0.4.6
fastapi==0.88.0
gunicorn==20.1.0
h11==0.14.0
idna==3.4
pydantic==1.10.4
sniffio==1.3.0
starlette==0.22.0
typing_extensions==4.4.0
uvicorn==0.20.0

Now use the -r switch in PIP install command.

pip3 install -r requirements.txt

The PIP utility is used with along with following commands −

pip uninstall

This command is used to uninstall one or more packages already installed.

Syntax

pip3 uninstall package, [package2, package3, . . ]

This will uninstall the packages along with the dependencies.

Example

pip3 uninstall flask

You will be asked confirmation for removal before proceeding.

pip3 uninstall flask
Found existing installation: Flask 2.2.3
Uninstalling Flask-2.2.3:
   Would remove:
   c:\python311\lib\site-packages\flask-2.2.3.dist-info\*
   c:\python311\lib\site-packages\flask\*
   c:\python311\scripts\flask.exe
Proceed (Y/n)?

pip list

This command gives a list installed packages, including editables. Packages are listed in a case-insensitive sorted order.

Syntax

pip3 list

Following switches are available with pip list command −

-o, --outdated: List outdated packages

pip3 list --outdated
Package     Version    Latest     Type
--------    -------    -------    -----
debugpy     1.6.6      1.6.7      wheel
ipython     8.11.0     8.12.0     wheel
pip         22.3.1     23.0.1     wheel
Pygments    2.14.0     2.15.0     wheel
setuptools  65.5.0     67.6.1     wheel

-u, --uptodate : List uptodate packages

pip3 list --uptodate
Package           Version
--------          --------- -------
click             8.1.3
colorama          0.4.6
executing         1.2.0
Flask             2.2.3
jedi              0.18.2
Jinja2            3.1.2
python-dateutil   2.8.2
pyzmq             25.0.2
six               1.16.0
Werkzeug          2.2.3

pip show

This command shows information about one or more installed packages. The output is in RFC-compliant mail header format.

Syntax

pip3 show package

Example

pip3 show flask
Name: Flask
Version: 2.2.3
Summary: A simple framework for building complex web applications.
Home-page: https://palletsprojects.com/p/flask
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: C:\Python311\Lib\site-packages
Requires: click, itsdangerous, Jinja2, Werkzeug
Required-by:

pip freeze

This command outputs installed packages in requirements format. All the packages are listed in a case-insensitive sorted order.

Syntax

pip3 freeze

The output of this command can be redirected to a text file with the following command −

pip3 freeze > requirements.txt

pip download

This command downloads packages from −

  • PyPI (and other indexes) using requirement specifiers.

  • VCS project urls.

  • Local project directories.

  • Local or remote source archives.

In fact, pip download does the same resolution and downloading as pip install, but instead of installing the dependencies, it collects the downloaded distributions into the directory provided (defaulting to the current directory). This directory can later be passed as the value to pip install --find-links to facilitate offline or locked down package installation.

Syntax

pip3 download somepackage

pip search

This command searches for PyPI packages whose name or summary contains the given query.

Syntax

pip3 search query

pip config

This command is used to manage local and global configuration.

Subcommands

  • list − List the active configuration (or from the file specified).

  • edit − Edit the configuration file in an editor.

  • get − Get the value associated with command.option.

  • set − Set the command.option=value.

  • unset − Unset the value associated with command.option.

  • debug − List the configuration files and values defined under them.

Configuration keys should be dot separated command and option name, with the special prefix "global" affecting any command.

Example

pip config set global.index-url https://example.org/

This would configure the index url for all commands.

pip config set download.timeout 10

This would configure a 10 second timeout only for "pip download" commands.

Advertisements