Skip to content

Commit 01f6217

Browse files
author
Chris Schneider
committed
Use built-in pkg_resources to get installed libraries
Previously, we used internal pip code, which was changing in point releases, causing issues. We actually don't need pip to do this, we can instead ask the installed python's pkg_resources directly for the same info. Related issues: pypa/pip#5079 pypa/pip#5080 pypa/pip#5081
1 parent 620f075 commit 01f6217

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

src/scout_apm/core/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
from __future__ import absolute_import
22

3-
4-
# pip needs to be imported before anything else (in particular the requests
5-
# library), since it vendors stuff oddly.
6-
#
7-
# This is an unsupported use of pip, and we need to figure out a smoother way
8-
# to detect loaded libraries. Check metadata.py for our usage.
9-
import pip # noqa
10-
113
# Python Modules
124
import logging
135
from os import getpid

src/scout_apm/core/metadata.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from datetime import datetime
55
import logging
66
from os import getpid
7-
import pip
87
import sys
98

109

@@ -44,7 +43,7 @@ def data(cls):
4443
'database_engine': '', # Detected
4544
'database_adapter': '', # Raw
4645
'application_name': '', # Environment.application_name,
47-
'libraries': cls.package_list(),
46+
'libraries': cls.get_python_packages_versions(),
4847
'paas': '',
4948
'git_sha': ''} # Environment.git_revision_sha()}
5049
except Exception as e:
@@ -53,16 +52,13 @@ def data(cls):
5352
return data
5453

5554
@classmethod
56-
def package_list(cls):
57-
packages = []
55+
def get_python_packages_versions(cls):
5856
try:
59-
for pkg_dist in [p for p in pip._vendor.pkg_resources.working_set]:
60-
try:
61-
p_split = str(pkg_dist).split()
62-
packages.append([p_split[0], p_split[1]])
63-
except Exception as e:
64-
logger.debug('Exception while reading packages: %s', repr(e))
65-
continue
66-
except Exception as e:
67-
logger.debug('Exception while getting pkg_resources: %s', repr(e))
68-
return packages
57+
import pkg_resources
58+
except ImportError:
59+
return []
60+
61+
return list(sorted(
62+
(distribution.project_name, distribution.version)
63+
for distribution in pkg_resources.working_set
64+
))

0 commit comments

Comments
 (0)