To install packages in Linux using python, you need to use "subprocess" module. Below is a script to install multiple packages.
import subprocess
def rpm_install(package_list):
for package in package_list:
try:
subprocess.run(['sudo', 'yum', 'install', '-y', package], check=True)
# For DNF, use the following line instead:
# subprocess.run(['sudo', 'dnf', 'install', '-y', package], check=True)
print(f"Package {package} installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to install {package}. Error: {e}")
# List of RPM packages to install
packages_to_install = ['package1', 'package2', 'package3']
# Call the function with the list of packages
rpm_install(packages_to_install)
The script defines a function named "rpm_install" that take a list of rpms from "packages_to_install" and later we have call that function.