Skip to content

Commit 1b52759

Browse files
committed
Speed up imports by using importlib instead of pkg_resources
Speed up imports by up to a second by replacing uses of `pkg_resources` with the new Python standard library module `importlib.resources` (or, for Python < 3.7, the backport `importlib_resources`). The old `pkg_resources` module is known to be slow because it does a lot of work on startup. See, for example, [pypa/setuptools#926](pypa/setuptools#926) and [pypa/setuptools#510](pypa/setuptools#510).
1 parent bbb2097 commit 1b52759

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

wheel/setup.py.in

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ lal_data_path_fixup = '''
6262
# This section was added automatically to support using LALSuite as a wheel.
6363
#
6464
import os
65-
import pkg_resources
66-
new_path = pkg_resources.resource_filename('lalapps', 'data')
65+
try:
66+
from importlib import resources
67+
except ImportError:
68+
# FIXME: remove after dropping support for Python < 3.7
69+
import importlib_resources as resources
70+
with resources.path('lalapps', '') as new_path:
71+
new_path = str(new_path / 'data')
6772
path = os.environ.get('LAL_DATA_PATH')
6873
path = path.split(':') if path else []
6974
if new_path not in path:
@@ -90,8 +95,15 @@ class build_py(_build_py):
9095

9196
stub = '''\
9297
#!python
93-
import os, pkg_resources, sys
94-
os.execv(pkg_resources.resource_filename('lalapps', 'bin/{}'), sys.argv)
98+
import os
99+
try:
100+
from importlib import resources
101+
except ImportError:
102+
# FIXME: remove after dropping support for Python < 3.7
103+
import importlib_resources as resources
104+
with resources.path('lalapps', 'bin') as new_path:
105+
new_path = str(new_path / '{}')
106+
os.execv(new_path, sys.argv)
95107
'''
96108

97109

@@ -172,5 +184,6 @@ setup(
172184
'lalinference': ['gwpy', 'gwdatafind']
173185
},
174186
install_requires=['lscsoft-glue', 'ligo-segments', 'matplotlib',
175-
'numpy>=1.7', 'python-dateutil', 'scipy']
187+
'numpy>=1.7', 'python-dateutil', 'scipy',
188+
'importlib_resources;python_version<"3.7"']
176189
)

0 commit comments

Comments
 (0)