diff options
author | Doug Hellmann <doug@doughellmann.com> | 2020-07-12 18:42:40 -0400 |
---|---|---|
committer | Doug Hellmann <doug@doughellmann.com> | 2020-07-12 18:42:40 -0400 |
commit | 859d45bf8f7254858fbfb6167fe073479e712cfd (patch) | |
tree | e541266f1a89ce0619dbe7645ee7d964d8848945 /plugins | |
parent | 6e19fb191dc0dd82416a9e9cc0b8e4a0ab5f200c (diff) | |
download | cmd2-git-859d45bf8f7254858fbfb6167fe073479e712cfd.tar.gz |
replace pkg_resources with importlib.metadata
Importing pkg_resources has a side-effect of scanning every installed
distribution on sys.path to load the metadata, especially the entry
points defined in the packages. This can have a significant
launch-time cost for command line applications when there are a lot of
distributions to scan.
Since cmd2 is only using pkg_resources to find the version of the
installed package, pkg_resources can be replaced with
importlib.metadata. The implementation in the new library is
significantly faster because it goes immediately to the metadata file
for the requested distribution, instead of scanning all of them. There
are also no import-time side-effects.
importlib.metadata is a new standard library module starting with
python 3.8. For earlier versions, a compatible library has been
released to PyPI as 'importlib_metadata'. This change adds the new
dependency with a qualifier so that it is only applied to older
versions of python, and then updates the places that were importing
pkg_resources to look for the different versions of the new library
instead. The documentation configuration is changed to import cmd2
itself to get its version, since the package has to be installed for
the metadata to be available anyway.
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/ext_test/cmd2_ext_test/__init__.py | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/plugins/ext_test/cmd2_ext_test/__init__.py b/plugins/ext_test/cmd2_ext_test/__init__.py index dc52b4d9..dbbc4250 100644 --- a/plugins/ext_test/cmd2_ext_test/__init__.py +++ b/plugins/ext_test/cmd2_ext_test/__init__.py @@ -5,16 +5,20 @@ An overview of what myplugin does. """ -from pkg_resources import DistributionNotFound, get_distribution +try: + # For python 3.8 and later + import importlib.metadata as importlib_metadata +except ImportError: + # For everyone else + import importlib_metadata +try: + __version__ = importlib_metadata.version(__name__) +except importlib_metadata.PackageNotFoundError: + # package is not installed + __version__ = 'unknown' from .cmd2_ext_test import ExternalTestMixin __all__ = [ 'ExternalTestMixin' ] - - -try: - __version__ = get_distribution(__name__).version -except DistributionNotFound: - __version__ = 'unknown' |