diff options
author | Mike <mikeh@blur.com> | 2018-05-02 14:59:43 -0700 |
---|---|---|
committer | Ashley Camba <ashwoods@gmail.com> | 2018-05-10 22:02:19 +0200 |
commit | 6dbfe1079a3919c6aa4ace467951cb0cf9950cbf (patch) | |
tree | 634f5a40d00a988b2936bea4f7a65ac8917cccf0 | |
parent | c00cbda5b3dd6027ef54d26fda05dc30494d1189 (diff) | |
download | raven-6dbfe1079a3919c6aa4ace467951cb0cf9950cbf.tar.gz |
Lazily import pkg_resources for better import performance.
-rw-r--r-- | raven/utils/__init__.py | 15 | ||||
-rw-r--r-- | raven/versioning.py | 13 |
2 files changed, 15 insertions, 13 deletions
diff --git a/raven/utils/__init__.py b/raven/utils/__init__.py index 91623b2..8d7c540 100644 --- a/raven/utils/__init__.py +++ b/raven/utils/__init__.py @@ -8,10 +8,6 @@ raven.utils from __future__ import absolute_import import logging -try: - import pkg_resources -except ImportError: - pkg_resources = None # NOQA import sys # Using "NOQA" to preserve export compatibility @@ -32,9 +28,16 @@ _VERSION_CACHE = {} def get_version_from_app(module_name, app): version = None - # Try to pull version from pkg_resource first + # Try to pull version from pkg_resources first # as it is able to detect version tagged with egg_info -b - if pkg_resources is not None: + try: + # Importing pkg_resources can be slow, so only import it + # if we need it. + import pkg_resources + except ImportError: + # pkg_resource is not available on Google App Engine + pass + else: # pull version from pkg_resources if distro exists try: return pkg_resources.get_distribution(module_name).version diff --git a/raven/versioning.py b/raven/versioning.py index 42f179f..8e1a262 100644 --- a/raven/versioning.py +++ b/raven/versioning.py @@ -2,12 +2,6 @@ from __future__ import absolute_import import os.path -try: - import pkg_resources -except ImportError: - # pkg_resource is not available on Google App Engine - pkg_resources = None - from raven.utils.compat import text_type from .exceptions import InvalidGitRepository @@ -68,7 +62,12 @@ def fetch_package_version(dist_name): """ >>> fetch_package_version('sentry') """ - if pkg_resources is None: + try: + # Importing pkg_resources can be slow, so only import it + # if we need it. + import pkg_resources + except ImportError: + # pkg_resource is not available on Google App Engine raise NotImplementedError('pkg_resources is not available ' 'on this Python install') dist = pkg_resources.get_distribution(dist_name) |