See when packages were installed / updated using pip

后端 未结 7 1700
说谎
说谎 2020-12-13 06:32

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 w

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 06:45

    Solution 1 : packages.date.py :

    import os
    import time
    from pip._internal.utils.misc import get_installed_distributions
    
    for package in get_installed_distributions():
         print (package, time.ctime(os.path.getctime(package.location)))
    

    Solution 2 : packages.alt.date.py :

    #!/usr/bin/env python
    # Prints when python packages were installed
    from __future__ import print_function
    from datetime import datetime
    from pip._internal.utils.misc import get_installed_distributions
    import os
    
    if __name__ == "__main__":
        packages = []
        for package in 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))
    

    Solution 1 & 2 compatibility :

    • updated solution for pip v10.x
    • python v2, v2.7, v3, v3.5, v3.7

提交回复
热议问题