Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See when packages were installed / updated using pip

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?

like image 950
Nishant Nawarkhede Avatar asked Jul 14 '14 12:07

Nishant Nawarkhede


People also ask

How do I see recently installed packages in Python?

The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages. You can also use the ActiveState Platform's command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command.

How do I see what has been installed on pip?

If you want to list all the Python packages installed in an environment, pip list command is what you are looking for. The command will return all the packages installed, along with their specific version and location. If a package is installed from a remote host (for example PyPI or Nexus) the location will be empty.

How do I know if pip is updated?

you can check the version of the pip by. pip --version. if you want to install any particular version of pip , for example version 18.1 then use this command, python -m pip install pip==18.1.


3 Answers

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.

like image 190
dersvenhesse Avatar answered Oct 22 '22 00:10

dersvenhesse


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))
like image 31
jof Avatar answered Oct 22 '22 01:10

jof


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
like image 11
intika Avatar answered Oct 22 '22 00:10

intika