How to Use pip list and 4 Options

In this post, we’ll explore how to use the pip list command to check the installed packages on your system and discuss its useful options. Python is a powerful programming language that allows you to utilize a wide range of packages and libraries. To efficiently manage these packages, you use a tool called pip. pip is a command-line tool that installs, updates, and removes packages from the Python Package Index (PyPI). We’ll also look at how to check installed packages and some additional usage tips.

Basic Usage of pip list

The pip list command displays a list of all Python packages installed on your system. By using this command, you can quickly see which packages are currently installed. The basic usage is as follows:

pip list

When you enter the above command, the terminal will display a list of installed packages along with their version information. The output will look like this:

Figure 1. Checking installed Python packages with pip list
Figure 1. Checking installed Python packages with pip list

As shown, the package names and their versions are listed side by side. pip list is a very useful tool for package management, as it helps you check for version conflicts or remove unnecessary packages.

Key Options for pip list

In addition to simply listing packages, pip list provides a variety of options to deliver more detailed information. These options allow for more granular control in specific situations. Let’s take a look at some of the useful options.

–outdated: Checking for Packages That Need Updates

The --outdated option shows you which of your installed packages need updates. With this option, you can easily identify outdated packages and update them to the latest versions.

pip list --outdated

Running this command will output something like this:

Figure 2. Checking for packages that need updates with pip list --outdated
Figure 2. Checking for packages that need updates with pip list –outdated

In the result above, Version shows the currently installed version, while Latest indicates the newest available version. This option is extremely useful for managing updates as it allows you to quickly identify packages with available updates.

–uptodate: Verifying Packages That Are Up to Date

The --uptodate option is used to verify packages that are already up to date. It’s helpful when you want to check which of your installed packages do not require updates.

pip list --uptodate

When you run this command, only packages that are up to date will be displayed. This helps you quickly assess the state of your packages and avoid unnecessary updates.

Figure 3. Verifying packages that are up to date with pip list --uptodate
Figure 3. Verifying packages that are up to date with pip list –uptodate

–format: Specifying Output Format

pip list allows you to change the output format in various ways. While the default is a table format, it can be displayed in other formats as well. For example, using the --format=columns option aligns the output in column format.

pip list --format=columns

Using this command will give you the following output, which is actually the default format even if you don’t use this option:

Figure 4. Outputting in column format with pip list --format=columns
Figure 4. Outputting in column format with pip list –format=columns

–format=freeze: Saving to a requirements.txt File

There is a way to save installed packages to a requirements file (requirements.txt). This method is very useful when you want to reproduce your project in another environment. To save installed packages to a requirements.txt file, use the pip list --format=freeze command.

pip list --format=freeze > requirements.txt

This command saves the currently installed packages and their versions to a requirements.txt file. You can later use this file to install the same packages in another environment. The requirements.txt file is recorded in the following format:

Figure 5. Creating a requirements.txt file with the --format=freeze option in pip list
Figure 5. Creating a requirements.txt file with the –format=freeze option in pip list

–format=json: Outputting in JSON Format

Outputting values in JSON format makes it convenient for programming, as you can directly read the values as JSON without the need to parse strings.

Figure 6. Outputting in JSON format with the --format=json option in pip list
Figure 6. Outputting in JSON format with the –format=json option in pip list

–not-required: Checking for Unused Packages

The --not-required option is used to check for packages that are not dependencies of other packages, meaning they were installed independently. This option is very useful when you want to clean up unnecessary packages from your system.

pip list --not-required

Running this command will yield results like the following:

Figure 7. Checking for unused packages with the --not-required option in pip list
Figure 7. Checking for unused packages with the –not-required option in pip list

This result shows packages that are not required by any other packages. By identifying these packages, you can remove unnecessary ones and keep your system clean.

Precautions When Using pip list

While the pip list command is very useful, there are a few precautions to keep in mind.

  • Using in a Virtual Environment: Python packages can be installed system-wide or only for specific projects. It’s recommended to run the pip list command within a virtual environment. A virtual environment is a tool that allows you to install and manage packages needed only for a specific project. Installing packages outside of a virtual environment can cause conflicts with other projects.
  • Permission Issues: If you are using pip list to install or remove packages, you may need administrator privileges. It is common to use the sudo command when installing packages system-wide. However, within a virtual environment, such permission issues do not occur, so it is recommended to use a virtual environment.
  • Package Management: Be cautious when removing unnecessary packages from the list displayed by pip list. Removing the wrong packages can cause dependency issues, which may affect the entire project.

Summary

The pip list command is an essential tool for Python developers to manage installed packages. With this command, you can check installed packages and their versions, and identify packages that need updates. Additionally, various options allow for more detailed package management.

In this post, we explored the basic usage of pip list along with its useful options. By properly utilizing this command, managing Python packages can become much more convenient. It is recommended to regularly use the pip list command and its options to manage your Python environment more efficiently.

References

Leave a Comment