Get date and time of installation for packages ins

2019-04-24 18:10发布

I would like to find a way to get the calendar date and time in hour:minute:seconds format for my packages installed via pip.

I would like to be able to see something in output like: Month/Day/Year - Hour:Minute:Seconds for each package.

Thanks!

2条回答
手持菜刀,她持情操
2楼-- · 2019-04-24 18:29

Is this what you are looking for -

import pip
import os
import time

In [139]: for package in pip.get_installed_distributions():
   .....:          print "%s: %s" % (package, time.ctime(os.path.getctime(package.location)))
   .....:     
pyudev 0.17.dev20150317: Tue Mar 17 12:02:58 2015
python-magic 0.4.6: Fri Mar 20 14:07:59 2015
runipy 0.1.0: Fri Oct 31 01:49:34 2014

Source of the code - https://stackoverflow.com/a/24736563/170005

You can do import pip too, which is pretty interesting. I didn't know this.

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-24 18:31

You can list all the locations that holds the packages and then just list all the files in these directories (together with the creation time):

import pip
import os
import time

pkg_location_dir_strset = set()

for pip_pkg in pip.get_installed_distributions():
    if pip_pkg.location not in pkg_location_dir_strset:
        pkg_location_dir_strset.add(pip_pkg.location)

for pkg_location_dir_str in pkg_location_dir_strset:
    print("")
    print("Directory: " + pkg_location_dir_str)
    for file_or_dir in os.listdir(pkg_location_dir_str):
        # print("file_or_dir = " + file_or_dir)
        file_or_dir_path = os.path.join(pkg_location_dir_str, file_or_dir)
        print(
            os.path.basename(file_or_dir).ljust(50)
            + " " + time.ctime(os.path.getctime(file_or_dir_path))
        )

Also check out this answer for an alternative solution which you may prefer

Hope this helps!

查看更多
登录 后发表回答