What Is "pip/pip2/pip3" Command

This section provides a tutorial example on how to use 'pip/pip2/pip3' command to install and manage third party Python packages.

What Is "pip/pip2/pip3" Command? "pip (Package Installer for Python)" is command line tool that allows you to manage party packages in your Python environment.

"pip2" command invokes the Python 2 version of the "pip" command, if you have multiple Python versions installed on your computer.

"pip3" command invokes the Python 3 version of the "pip" command, if you have multiple Python versions installed on your computer.

"pip" is installed automatically as part of your Python environment. It should be accessible by entering "pip" at the command line prompt.

Here are some "pip" command examples in my Python environment on a Linux computer.

0. Identify Python version of the default "pip" command. The output shows that "pip" is the same as "pip3" for Python 3 on my computer.

herong$ pip --version
pip 21.0.1 from /usr/lib/python3.8/site-packages/pip (python 3.8)

herong$ pip3 --version
pip 21.0.1 from /usr/lib/python3.8/site-packages/pip (python 3.8)

herong$ pip2 --version
pip 9.0.3 from /usr/lib/python2.7/site-packages (python 2.7)

1. Show "pip" command options to manage Python packages.

herong$ pip help

Usage:
  pip <command> [options]

Commands:
  install          Install packages.
  download         Download packages.
  uninstall        Uninstall packages.
  freeze           Output installed packages in requirements format.
  list             List installed packages.
  show             Show information about installed packages.
  check            Verify installed packages have compatible dependencies.
  search           Search PyPI for packages.
  wheel            Build wheels from your requirements.
  hash             Compute hashes of package archives.
  completion       A helper command used for command completion.
  help             Show help for commands.

General Options:
  -h, --help       Show help.
  --isolated       Run pip in an isolated mode, ignoring environment
                   variables and user configuration.
  -v, --verbose    Give more output. Option is additive, and can be
                   used up to 3 times.
  -V, --version    Show version and exit.
  -q, --quiet      Give less output. Option is additive, and can be
                   used up to 3 times (corresponding to
                   WARNING, ERROR, and CRITICAL logging levels).
  --log <path>     Path to a verbose appending log.

2. Get a list of installed Python packages:

herong$ pip list

asn1crypto (0.24.0)
blivet (3.1.0)
Brlapi (0.6.7)
cffi (1.11.5)
chardet (3.0.4)
chrome-gnome-shell (0.0.0)
configobj (5.0.6)
coverage (4.5.1)
cryptography (2.3)
...

3. Search for a package to handle JSON strings. I see a list of JSON packages. I am not sure which is better.

herong$ pip search json

json-escape (1.0.0)              - Json escape
json-websocket (0.2.1582880480)  - json-websocket
json-dotenv (0.0.21)             - json-dotenv
mo-json (3.57.20089)             - More JSON Tools!
json-mindiff (0.1)               - Minimize json diffs
linaro-json (2.0.1)              - JSON Schema Validator
json-logging (1.0.6)             - JSON Python Logging
json-repository (0.2.0)          - A simple json repository
pygments-json (0.1)              - Pygments JSON formatter
pretty-json (1.2.0)              - make pretty json
...

4. Sometimes, "pip" command may fail due to network connection issue. Just try it again later.

herong$ pip search json
Retrying (Retry(total=4, connect=None, read=None, redirect=None, ...
...
Exception:
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/connection.py"
  ...
OSError: [Errno 101] Network is unreachable

5. If you think your "pip" command is outdated, you can use "pip" to upgrade itself.

herong$ sudo pip install --upgrade pip

6. Run "pip" with the "python -m pip" command. This works because "pip" is actually a Python module.

herong$ python -m pip help

Usage:
  /usr/bin/python -m pip <command> [options]
  ...

7. If you have multiple Python versions installed, you can use "pip2" or "pip3" to manage packages for different Python versions.

herong$ pip --version
pip 21.0.1 from /usr/lib/python3.8/site-packages/pip (python 3.8)

herong$ pip3 --version
pip 21.0.1 from /usr/lib/python3.8/site-packages/pip (python 3.8)

herong$ pip2 --version
pip 9.0.3 from /usr/lib/python2.7/site-packages (python 2.7)

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

"pip" - Package Installer for Python

What Is "pip/pip2/pip3" Command

 Use "pip" Package in Python Script

 Install MySQL Connector/Python with "pip"

 Using "pip" with Different Sources

 Commonly Used Python Packages/Libraries

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB