Python Script to list Installed packages with installation dates

python @ Freshers.in

To find the last installed libraries in Python, you can use the pip tool, which is the package manager for Python. You can use the following steps:

Open a terminal or command prompt.

Run the following command to list all installed packages along with their installation dates:

pip list --format=columns --verbose | sort -r

This command will display a list of installed packages in descending order of installation date and time, with the most recently installed packages at the top.

Here’s what the command does:

pip list: This command lists all installed packages.
–format=columns: This option formats the output in columns for better readability.
–verbose: This option provides additional information, including the installation date.
sort -r: This part of the command sorts the list in reverse order, so the most recently installed packages appear first.

pip list command does not include the installation date by default. To find the installation date of Python packages, you can use the pip-show package and a Python script. Here’s how you can do it:

First, install the pip-show package if you haven’t already

pip install pip-show

After installing pip-show, you can create a Python script to list installed packages along with their installation dates. Create a file, e.g., list_installed_packages.py, and add the following code:

import subprocess
from datetime import datetime

def get_installed_packages():
    # Run 'pip freeze' to get a list of installed packages
    result = subprocess.run(['pip', 'freeze'], stdout=subprocess.PIPE, text=True)
    installed_packages = result.stdout.strip().split('\n')
    # Create a dictionary to store package names and installation dates
    package_info = {}
    for package in installed_packages:
        package_name = package.split('==')[0]
        result = subprocess.run(['pip-show', package_name], stdout=subprocess.PIPE, text=True)
        package_data = result.stdout.strip().split('\n')
        for line in package_data:
            if line.startswith('Location:'):
                location = line.split(' ')[-1].strip()
            if line.startswith('Installer:'):
                installer = line.split(' ')[-1].strip()
            if line.startswith('Metadata-Version:'):
                # Use metadata version as a proxy for installation date
                metadata_version = line.split(' ')[-1].strip()
        try:
            # Convert metadata version to a datetime object
            install_date = datetime.strptime(metadata_version, '%Y-%m-%dT%H:%M:%S.%f')
        except ValueError:
            # Handle cases where metadata version is not in the expected format
            install_date = None
        package_info[package_name] = {
            'Location': location,
            'Installer': installer,
            'Install Date': install_date,
        }
    return package_info
if __name__ == '__main__':
    installed_packages = get_installed_packages()
    for package_name, info in installed_packages.items():
        print(f'Package: {package_name}')
        print(f'Location: {info["Location"]}')
        print(f'Installer: {info["Installer"]}')
        print(f'Install Date: {info["Install Date"]}\n')

Save the script, and then run it:

python list_installed_packages.py

Refer more on python here :

Author: user

Leave a Reply