Get date and time of installation for packages installed via pip

Get much more than just date & time, but also install method, using pip-date

enter image description here


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!


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.


As of version 10.0.0 the get_installed_distributions() is not accessible from its previous location.

Although it is not recommended to use pip as a module inside your program, sometimes -like this question- it makes things much easier!

So, I searched the libraries a bit and found where it is hiding :) Here is the @fixxxer's answer according to new API and compatible with Python 3.x:

import os
import time
from pip._internal.utils.misc import get_installed_distributions

for package in get_installed_distributions():
    print ("{}\t{}".format(time.ctime(os.path.getctime(package.location)), package))