from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from pkg_resources import get_distribution, DistributionNotFound


def check(request):
    package_name = settings.HEARTBEAT.get('package_name')
    if not package_name:
        raise ImproperlyConfigured(
            'Missing package_name key from heartbeat configuration')

    try:
        distro = get_distribution(package_name)
    except DistributionNotFound:
        return dict(error='no distribution found for {}'.format(package_name))

    return dict(name=distro.project_name, version=distro.version)

from pkg_resources import Requirement, WorkingSet

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured


def check(request):
    package_name = settings.HEARTBEAT.get('package_name')
    if not package_name:
        raise ImproperlyConfigured(
            'Missing package_name key from heartbeat configuration')

    sys_path_distros = WorkingSet()
    package_req = Requirement.parse(package_name)

    distro = sys_path_distros.find(package_req)
    if not distro:
        return dict(error='no distribution found for {}'.format(package_name))

    return dict(name=distro.project_name, version=distro.version)

Package name should be searched through the same distros list we use for listing installed packages. get_distribution is using the global working_set which may not contain the requested package if the initialization(pkg_resources import time) happened before the project name to appear in the sys.path.
Package name should be searched through the same distros list
we use for listing installed packages. get_distribution is using
the global working_set which may not contain the requested package
if the initialization(pkg_resources import time) happened before
the project name to appear in the sys.path.

+ from pkg_resources import Requirement, WorkingSet
+ 
  from django.conf import settings
  from django.core.exceptions import ImproperlyConfigured
- from pkg_resources import get_distribution, DistributionNotFound
  
  
  def check(request):
      package_name = settings.HEARTBEAT.get('package_name')
      if not package_name:
          raise ImproperlyConfigured(
              'Missing package_name key from heartbeat configuration')
  
-     try:
-         distro = get_distribution(package_name)
-     except DistributionNotFound:
+     sys_path_distros = WorkingSet()
+     package_req = Requirement.parse(package_name)
+ 
+     distro = sys_path_distros.find(package_req)
+     if not distro:
          return dict(error='no distribution found for {}'.format(package_name))
  
      return dict(name=distro.project_name, version=distro.version)
  
Package name should be searched through the same distros list we use for listing installed packages. get_distribution is using the global working_set which may not contain the requested package if the initialization(pkg_resources import time) happened before the project name to appear in the sys.path.
## Code Before:
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from pkg_resources import get_distribution, DistributionNotFound


def check(request):
    package_name = settings.HEARTBEAT.get('package_name')
    if not package_name:
        raise ImproperlyConfigured(
            'Missing package_name key from heartbeat configuration')

    try:
        distro = get_distribution(package_name)
    except DistributionNotFound:
        return dict(error='no distribution found for {}'.format(package_name))

    return dict(name=distro.project_name, version=distro.version)

## Instruction:
Package name should be searched through the same distros list we use for listing installed packages. get_distribution is using the global working_set which may not contain the requested package if the initialization(pkg_resources import time) happened before the project name to appear in the sys.path.
## Code After:
from pkg_resources import Requirement, WorkingSet

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured


def check(request):
    package_name = settings.HEARTBEAT.get('package_name')
    if not package_name:
        raise ImproperlyConfigured(
            'Missing package_name key from heartbeat configuration')

    sys_path_distros = WorkingSet()
    package_req = Requirement.parse(package_name)

    distro = sys_path_distros.find(package_req)
    if not distro:
        return dict(error='no distribution found for {}'.format(package_name))

    return dict(name=distro.project_name, version=distro.version)
