A pragmatic way to talk with pypi and find out what dependencies are out of date
/opt/conda/lib/python3.9/site-packages/_distutils_hack/__init__.py:30: UserWarning: Setuptools is replacing distutils.
  warnings.warn("Setuptools is replacing distutils.")

Dependency Traversing

Sometimes, we may want to check the current installed versions of a project's basic dependencies, and further check if those dependencies are out of date. dependency_checker is designed around this concept, utilizing the pipdeptree library.

get_installed_dependencies[source]

get_installed_dependencies(package_name:str, depth_limit:int=1, include_self:bool=False)

Recursively grabs dependencies of python package

Type Default Details
package_name str The name of a python package
depth_limit int 1 How deep to follow nested dependencies
include_self bool False Whether to include the original library in the results

This function operates by traversing a DAG and grabbing dependencies of projects found from it. Generally a depth of 1 is recommended, below is a quick guide to what will be returned at each depth.

0: A depth of zero will an empty dictionary unless include_self is True. If so, it will include only the library name:

deps = get_installed_dependencies('pipdeptree', depth_limit=0)
assert deps == {}
deps = get_installed_dependencies('pipdeptree', depth_limit=0, include_self=True)
assert deps == {'pipdeptree':'2.2.1'}

1: A depth of one will return the project and its main dependencies (if include_self is True), such as those stated in the requirements.txt as well as packages such as pip

deps = get_installed_dependencies('pipdeptree', depth_limit=1, include_self=True)
assert len(deps.keys()) == 2
assert all(package in deps.keys() for package in ('pipdeptree', 'pip'))
deps = get_installed_dependencies('pipdeptree', depth_limit=1, include_self=False)
assert len(deps.keys()) == 1
assert 'pip' in deps.keys()

2+: A depth of two or greater will return the dependencies for each of the dependencies above that layer. These allow for more fine-grained requirements

Checking for New Versions

Given these dependencies, we can also then check for a new version to see if an upgrade is available. This is what the is_latest_version function is designed for:

is_latest_version[source]

is_latest_version(package_name:str, current_version:str)

Compares the current version with the latest version, and returns if they are different

Type Default Details
package_name str The name of a pip python package
current_version str The installed version of a package, such as "1.2.3"
using_latest_version = is_latest_version('pipdeptree', '2.0.9')
assert using_latest_version == False

Here we tested if pipdeptree is the latest version. The version we specified is one less than that of the latest release at the time of development. We got False, meaning a newer version is available.

Checking the Release Notes

get_github_repo_from_pypi[source]

get_github_repo_from_pypi(name:str)

Gets the Github URL for a pypi package

Type Default Details
name str The name of a pypi package
assert get_github_repo_from_pypi("dependency_checker") == "muellerzr/dependency_checker"

get_latest_release_notes[source]

get_latest_release_notes(package:str)

Gets the URL for the latest release notes of a pypi package from Github if available

Type Default Details
package str The name of a pypi package
assert get_latest_release_notes("dependency_checker") == {}
assert list(get_latest_release_notes("torch").keys()) == ["notes_url", "release_tag"]
def _in_notebook():
    try:
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell':
            return True   # Jupyter notebook or qtconsole
        elif shell == 'TerminalInteractiveShell':
            return False  # Terminal running IPython
        else:
            return False  # Other type (?)
    except NameError:
        return False      # Probably standard Python interpreter

check_for_newer_release[source]

check_for_newer_release(package:str, version:str=None)

Checks if a newer release is available and then finds the release notes

Type Default Details
package str The name of a pypi package
version str None An optional version number

With this utility we can quickly get a pretty formatted print of the new version to install, how to install it, and the latest release notes as a link:

check_for_newer_release("torch", "1.9.0")

Newer version of torch was found available on pypi (1.9.0 -> v1.11.0)

To upgrade run pip install torch -U

Click Here to see the latest release notes.

False