Getting to know the Python installer

Command Line – pip3

© Photo by David Clode on Unsplash

© Photo by David Clode on Unsplash

Article from Issue 246/2021
Author(s):

As a replacement for pip, pip3 offers a complete solution for binary packages. Here's how to get started with this increasingly popular Python installer.

You can tell the popularity of Linux tools by how often they are used to install packages outside of distro repositories. For years, source packages have been installed by the trinity of commands configure, make, and make install for compiling. More recently, deb packages have become the norm, usually configured for Ubuntu. Both these installation choices are still popular, but, today, the popularity of the Python programming language is reflected in the increasing use of pip3 [1].

Pip3 is the Python installer for Python 3.x releases. It replaces pip, which is used for earlier versions of Python, as well as easy_install, and can also work with eggs [2] and wheels [3], two other standard Python tools designed to simplify packaging. In basic functionality, it is similar to package managers in other programming languages, such as npm in JavaScript or gem in Ruby. You should note, however that in a command, pip3 is often referenced as pip.

Like the packages in a distribution's repositories, pip3 automatically handles dependencies, installing them first to avoid the problems of a half-installed package. In addition, pip3 packages are simpler to build than their predecessors and include detailed error and warning messages, as well as a requirement file [4] that makes cloning installations on multiple machines easier. In fact, pip3 is so similar to deb and RPM that it is often talked about in terms of distributions and packages, a habit that causes considerable confusion. Regardless, pip3 has advantages for developers and users alike.

Recent distributions often install Python 3 along with pip3 and pip by default. You can check which you have by running:

python --version
pip3 --version

Some applications require a specific earlier version of Python, which you can download from the Python website [5]. If you need to upgrade pip3, run:

python3 -m pip install --upgrade pip

If you need to install or upgrade pip, you can usually install it with

python -m ensurepip --default-pip

or by one of the other means listed on the Python packaging page [6].

You may also want to sandbox the packages you install in a virtual environment. Python has several tools for creating a virtual environment, including virtualenv, pyvenv, and venv. In Python 3.4 and later, the preferred tool is venv [7], which may need to be installed separately. To create the virtual environment, first install Python and add the directory to your $Path variable if necessary. Then create the directory for the environment and activate it with:

python3 -m venv DIRECTORY-PATH
source DIRECTORY/activate

When you install a package or do any other work in Python, you will be using the specified virtual environment so long as you are in its directory until you type deactivate.

Using pip3

When a project under development uses pip3 to install, it will probably give the command to install its packages. However, you may also find packages to download in the Python repositories. The main general repository is the Python Package Index (PyPI, aka the Cheese Shop) [8]. However, there are dozens of others. Some are general repositories like Awesome Python. Others are concerned with special interests, such as scikit-learn and Python Design Patterns, or focus on major Python specific applications such as Ansible or Django.

The basic command structure for installing a package using pip3 depends on where the package is in relation to your machine. In each case, the -m option tells Python to search for the specified package, followed by the pip3 sub-command (install), the package, and then specifications for the version, if any. Pip for Python 2 uses a similar structure, but as of January 2021 it is no longer supported (Figure 1), although it may still work.

Figure 1: Starting in January 2021, Python 2 and pip are no longer officially supported. In Python 3, pip refers to what until now has been pip3.

There are several common installation choices. To install the latest version, use

python3 -m pip install "PACKAGE"

Specify the directory for the local package file, the URL for version control, or the Python repository. (The package name alone is needed for PyPI, as in Figure 2).

Figure 2: Since pip3 installs with a connection to PyPI, the basic installation command is simple.

To install a specific version, use:

python3 -m pip install "PACKAGE==VERSION"

To install a version that is greater than or equal to one version and less than another version, use:

python3 -m pip install "PACKAGE>=VERSION1,<HIGHER-VERSION"

To install a version compatible with a certain version, use:

python3 -m pip install "PACKAGE~=VERSION"

This is usually a point release of the version specified.

Notice that because a connection to PyPI automatically exists, only the package name needs to be specified for packages in PyPI. The same is true for local packages on $PATH or the current directory, or the directory for storing wheels (usually /usr/share/python-wheels). With the -requirement FILE (-r FILE) option, you can also use a requirement file for the package to install or the URL to a project's version control system (VCS).

Other formats are available to help users deal with the variety of different Python releases.

Besides install, pip3 supports a number of related commands:

  • uninstall: Removes a package
  • list: Displays all installed packages on a system (Figure 3)
Figure 3: The list command displays all Python packages installed.
  • show: Displays information about a package (Figure 4)
Figure 4: The show command gives detailed information about a package.
  • search: Locates a package on PYPI

In addition, pip3 includes several commands for building packages, such as freeze, which outputs a requirement file (Figure 5) or a list of dependencies that can be used in packaging or troubleshooting, and wheel, which creates a type of file for streamlining package installation. However, these commands are separate articles in themselves.

Figure 5: The requirement file for Django, showing the required dependencies.

You can also use options to refine a pip3 command. The --upgrade (-u) option ensures you install the very latest version of a package, although at times it may cause problems with dependencies. Similarly, you may want to use --eggs, for compatibility with older systems that have eggs installed. You can also install custom options with --install OPTION, such as:

--install-option ="--install-scripts=/usr/local/bin"

For custom requirement files, you can use --requirement FILE (-r).

Still other install options cover the usual range of options such as forcing a reinstall (--force-reinstall), not installing dependencies (--no-deps), or specifying the target directory for packages (-t, --target DIRECTORY). You can even use --pre to override pip3's default setting to search for only stable releases and search for pre-release and development builds.

When uninstalling, pip3 offers the option to delete all the packages in a requirement file with --requirement FILE (-r FILE). If necessary, multiple requirement files can be specified. With --yes (-y), pip3 will not ask for confirmation before deleting files. More general options are available as well, such as --verbose (-v), which has three levels of output, or --quiet (-q), which gives less output. You can also change the default logfile (~/.pip/pip.log), positioning a standard log with --log-path PATH or a verbose log with --log PATH.

The Next Stage

Pip3 is a complete solution for binary packages. It offers compatibility with many of the other package tools that Python has evolved over the years, and – just as importantly – does so while living up to Python's reputation for clarity and organization. You have only to contrast pip3 to Debian's apt-get to see what a difference paying attention to structure can make.

Probably, the information given here is more than enough to guide those unfamiliar with pip3. However, to give a complete picture, I should also mention setuptools [9], a general command for managing packages. While pip3 is more than enough for small-scale operations, such as when trying an application still in development, setuptools is better suited for large-scale operations. Should you find pip3 straining to meet your packaging needs, have a look at setuptools. If the popularity of Python continues to grow, I have a hunch that we will be seeing a lot more of both.

The Author

Bruce Byfield is a computer journalist and a freelance writer and editor specializing in free and open source software. In addition to his writing projects, he also teaches live and e-learning courses. In his spare time, Bruce writes about Northwest coast art (http://brucebyfield.wordpress.com). He is also co-founder of Prentice Pieces, a blog about writing and fantasy at https://prenticepieces.com/.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News