See when packages were installed / updated using pip

后端 未结 7 1624
说谎
说谎 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
    0 讨论(0)
  • 2020-12-13 06:50

    pip freeze gives you all the installed packages. Assuming you know the folder:

    time.ctime(os.path.getctime(file))

    should give you the creation time of a file, i.e. date of when the package has been installed or updated.

    0 讨论(0)
  • 2020-12-13 06:55

    I don't know all pip options but for one module you can get list of its files
    and then you can check its dates using python or bash.

    For example list of files in requests module

    pip show --files requests
    

    result:

    Name: requests
    Version: 2.2.1
    Location: /usr/local/lib/python2.7/dist-packages
    Requires: 
    Files:
      ../requests/hooks.py
      ../requests/status_codes.py
      ../requests/auth.py
      ../requests/models.py
    
    etc.
    

    BTW: you can use --help to see more options for some functions

    pip --help
    pip list --help
    pip show --help
    etc.
    
    0 讨论(0)
  • 2020-12-13 07:01

    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))
    
    0 讨论(0)
  • 2020-12-13 07:04

    You could use the --log option:

    --log <path>   Path to a verbose appending log. This log is inactive by default.
    

    E.g:

    $ pip install --log ~/.pip/pip.append.log gunicorn
    

    Or you can set it in your pip.conf to be enabled by default:

    [global]
    log = <path>
    

    Then all the pip operations will be logged verbosely into the specified file along with a log separator and timestamp, e.g.:

    $ pip install --log ~/.pip/pip.append.log gunicorn
    $ pip install  --log ~/.pip/pip.append.log --upgrade gunicorn
    

    logs the following to ~/.pip/pip.append.log:

    ------------------------------------------------------------
    /usr/bin/pip run on Mon Jul 14 14:35:36 2014
    Downloading/unpacking gunicorn
    ...
    Successfully installed gunicorn
    Cleaning up...
    ------------------------------------------------------------
    /usr/bin/pip run on Mon Jul 14 14:35:57 2014
    Getting page https://pypi.python.org/simple/gunicorn/
    URLs to search for versions for gunicorn in /usr/lib/python2.7/site-packages:
    * https://pypi.python.org/simple/gunicorn/
    ...
    Requirement already up-to-date: gunicorn in /usr/lib/python2.7/site-packages
    Cleaning up...
    

    You could parse out what you need from this log. While not the nicest it's a standard pip facility.

    0 讨论(0)
  • 2020-12-13 07:06

    If it's not necessary to differ between updated and installed, you can use the change time of the package file.

    Like that for Python 2 with pip < 10:

    import pip, os, time
    
    for package in pip.get_installed_distributions():
         print "%s: %s" % (package, time.ctime(os.path.getctime(package.location)))
    

    or like that for newer versions (tested with Python 3.7 and installed setuptools 40.8 which bring pkg_resources):

    import pkg_resources, os, time
    
    for package in pkg_resources.working_set:
        print("%s: %s" % (package, time.ctime(os.path.getctime(package.location))))
    

    an output will look like numpy 1.12.1: Tue Feb 12 21:36:37 2019 in both cases.

    Btw: Instead of using pip freeze you can use pip list which is able to provide some more information, like outdated packages via pip list -o.

    0 讨论(0)
提交回复
热议问题