Python: Run “pip list” through the program
Python: Running “pip list” Programmatically
In this article, we’ll explain how to use the Python programming language to run the “pip list” command programmatically. The “pip list” command lists all installed libraries and their version information in a Python environment and is often very useful when developing and deploying Python applications.
Read more: Python Tutorial
1. Running Commands with the subprocess Module
The subprocess module in Python provides the ability to execute external commands. We can use this module to run the “pip list” command. Here’s a simple example:
import subprocess
# Execute the `pip list` command
result = subprocess.run([‘pip’, ‘list’], capture_output=True, text=True)
# Print the execution result
print(result.stdout)
In the above example, we use the `subprocess.run()` function to execute the `pip list` command. This function accepts a list as an argument, the first element of which is the command to be executed, followed by the arguments. We set `capture_output` to `True` to capture the command’s output in the `result` variable. We also set `text` to `True` to return the output as text.
Executing the above code prints all installed libraries and their version information in the Python environment to the console.
2. Using the os Module to Run Commands
In addition to using the subprocess module, we can also use the os module to run commands. The os module provides command execution functionality and offers greater flexibility. Below is an example of using the os module to run the “pip list” command:
import os
# Execute the "pip list" command
result = os.popen('pip list').read()
# Print the execution result
print(result)
In the above example, we use the os.popen() function to execute the “pip list” command and read its output into the result variable. We then print the result.
3. Using the pip Module to Run Commands
In addition to running the “pip list” command directly, we can also use Python’s pip module to achieve the same functionality. The pip module is an official Python tool for managing Python packages. We can use it to execute the “pip list” command. Below is an example of using the pip module to run the “pip list” command:
from pip._internal.utils.misc import get_installed_distributions
# Get a list of installed packages
packages = get_installed_distributions()
# Print the package name and version information
for package in packages:
print(f"{package.key} ({package.version})")
In the above example, we import the get_installed_distributions() function from the pip module to obtain a list of installed packages and store it in the packages variable. We then use a loop to iterate over the packages list and print the name and version information for each package.
Summary
Through the above examples, we learned how to use the Python programming language to programmatically run the “pip list” command. We can use the subprocess module, the os module, or the pip module to execute this command and retrieve information about all installed libraries and their versions in the Python environment. This is extremely useful for developing and deploying Python applications, helping us understand the installed libraries and their versions in the current environment for version and dependency management. It also expands our possibilities for running commands, allowing us to more flexibly use Python on the command line.