See when packages were installed / updated using p

2019-01-22 18:37发布

I know how to see installed Python packages using pip, just use pip freeze. But is there any way to see the date and time when package is installed or updated with pip?

标签: python pip
7条回答
迷人小祖宗
2楼-- · 2019-01-22 19:26

Unfortunately, python packaging makes this a bit complicated since there is no consistent place that lists where the package files or module directories are placed.

Here's the best I've come up with:

#!/usr/bin/env python
# Prints when python packages were installed
from __future__ import print_function
from datetime import datetime
import os
import pip


if __name__ == "__main__":
    packages = []
    for package in pip.get_installed_distributions():
        package_name_version = str(package)
        try:
            module_dir = next(package._get_metadata('top_level.txt'))
            package_location = os.path.join(package.location, module_dir)
            os.stat(package_location)
        except (StopIteration, OSError):
            try:
                package_location = os.path.join(package.location, package.key)
                os.stat(package_location)
            except:
                package_location = package.location
        modification_time = os.path.getctime(package_location)
        modification_time = datetime.fromtimestamp(modification_time)
        packages.append([
            modification_time,
            package_name_version
        ])
    for modification_time, package_name_version in sorted(packages):
        print("{0} - {1}".format(modification_time,
                                 package_name_version))
查看更多
登录 后发表回答