Skip to content

Commit

Permalink
Speed up imports by using importlib instead of pkg_resources
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
lpsinger committed May 5, 2020
1 parent bbb2097 commit 1b52759
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions wheel/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ lal_data_path_fixup = '''
# This section was added automatically to support using LALSuite as a wheel.
#
import os
import pkg_resources
new_path = pkg_resources.resource_filename('lalapps', 'data')
try:
from importlib import resources
except ImportError:
# FIXME: remove after dropping support for Python < 3.7
import importlib_resources as resources
with resources.path('lalapps', '') as new_path:
new_path = str(new_path / 'data')
path = os.environ.get('LAL_DATA_PATH')
path = path.split(':') if path else []
if new_path not in path:
Expand All @@ -90,8 +95,15 @@ class build_py(_build_py):

stub = '''\
#!python
import os, pkg_resources, sys
os.execv(pkg_resources.resource_filename('lalapps', 'bin/{}'), sys.argv)
import os
try:
from importlib import resources
except ImportError:
# FIXME: remove after dropping support for Python < 3.7
import importlib_resources as resources
with resources.path('lalapps', 'bin') as new_path:
new_path = str(new_path / '{}')
os.execv(new_path, sys.argv)
'''


Expand Down Expand Up @@ -172,5 +184,6 @@ setup(
'lalinference': ['gwpy', 'gwdatafind']
},
install_requires=['lscsoft-glue', 'ligo-segments', 'matplotlib',
'numpy>=1.7', 'python-dateutil', 'scipy']
'numpy>=1.7', 'python-dateutil', 'scipy',
'importlib_resources;python_version<"3.7"']
)

0 comments on commit 1b52759

Please sign in to comment.