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:03

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-01-22 19:03

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.
查看更多
▲ chillily
4楼-- · 2019-01-22 19:04

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
查看更多
Summer. ? 凉城
5楼-- · 2019-01-22 19:08

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.

查看更多
Explosion°爆炸
6楼-- · 2019-01-22 19:13

I was recently looking for this too. But although there are many good answers here, the real issue is that since pip is not keeping logs by default, we have to resort to using the file creation and modification times, known as ctime and mtime, respectively. (See MAC-times.) Unfortunately, using this method has two side effects:

  1. Different OS's and FS's handles the ctime/mtime differently (if even available)
  2. Python installations are using many different directories, and some remain after installations while others are created on the fly when running. Making it hard to know exaclty what files to check the dates on.

However, there is a tool called pip-date that try to combine a few different methods.

pip install pip-date


enter image description here

查看更多
叛逆
7楼-- · 2019-01-22 19:14

If its not necessary to differ between updated and installed, you can use the change time of the package file. Like that:

import pip, os, time

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

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.

查看更多
登录 后发表回答