Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit 6dbfe10

Browse files
MHendricksashwoods
authored andcommitted
Lazily import pkg_resources for better import performance.
1 parent c00cbda commit 6dbfe10

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

raven/utils/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
from __future__ import absolute_import
99

1010
import logging
11-
try:
12-
import pkg_resources
13-
except ImportError:
14-
pkg_resources = None # NOQA
1511
import sys
1612

1713
# Using "NOQA" to preserve export compatibility
@@ -32,9 +28,16 @@
3228
def get_version_from_app(module_name, app):
3329
version = None
3430

35-
# Try to pull version from pkg_resource first
31+
# Try to pull version from pkg_resources first
3632
# as it is able to detect version tagged with egg_info -b
37-
if pkg_resources is not None:
33+
try:
34+
# Importing pkg_resources can be slow, so only import it
35+
# if we need it.
36+
import pkg_resources
37+
except ImportError:
38+
# pkg_resource is not available on Google App Engine
39+
pass
40+
else:
3841
# pull version from pkg_resources if distro exists
3942
try:
4043
return pkg_resources.get_distribution(module_name).version

raven/versioning.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
import os.path
44

5-
try:
6-
import pkg_resources
7-
except ImportError:
8-
# pkg_resource is not available on Google App Engine
9-
pkg_resources = None
10-
115
from raven.utils.compat import text_type
126
from .exceptions import InvalidGitRepository
137

@@ -68,7 +62,12 @@ def fetch_package_version(dist_name):
6862
"""
6963
>>> fetch_package_version('sentry')
7064
"""
71-
if pkg_resources is None:
65+
try:
66+
# Importing pkg_resources can be slow, so only import it
67+
# if we need it.
68+
import pkg_resources
69+
except ImportError:
70+
# pkg_resource is not available on Google App Engine
7271
raise NotImplementedError('pkg_resources is not available '
7372
'on this Python install')
7473
dist = pkg_resources.get_distribution(dist_name)

0 commit comments

Comments
 (0)