From f9786d765edf16afa092cf378a0a45fb321efe22 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Sun, 24 May 2020 14:48:56 +0200 Subject: [PATCH] Simplify is_package check --- spacy/cli/download.py | 18 ++---------------- spacy/util.py | 13 +++++-------- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/spacy/cli/download.py b/spacy/cli/download.py index 0230e272d..af132bbbe 100644 --- a/spacy/cli/download.py +++ b/spacy/cli/download.py @@ -5,6 +5,7 @@ import sys from wasabi import msg from .. import about +from ..util import is_package def download( @@ -17,7 +18,7 @@ def download( flag is set, the command expects the full model name with version. For direct downloads, the compatibility check will be skipped. """ - if not require_package("spacy") and "--no-deps" not in pip_args: + if not is_package("spacy") and "--no-deps" not in pip_args: msg.warn( "Skipping model package dependencies and setting `--no-deps`. " "You don't seem to have the spaCy package itself installed " @@ -45,21 +46,6 @@ def download( "Download and installation successful", f"You can now load the model via spacy.load('{model_name}')", ) - # If a model is downloaded and then loaded within the same process, our - # is_package check currently fails, because pkg_resources.working_set - # is not refreshed automatically (see #3923). We're trying to work - # around this here be requiring the package explicitly. - require_package(model_name) - - -def require_package(name): - try: - import pkg_resources - - pkg_resources.working_set.require(name) - return True - except: # noqa: E722 - return False def get_json(url, desc): diff --git a/spacy/util.py b/spacy/util.py index 5a7c633fa..41af881c9 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -341,14 +341,11 @@ def is_package(name): name (unicode): Name of package. RETURNS (bool): True if installed package, False if not. """ - import pkg_resources - - name = name.lower() # compare package name against lowercase name - packages = pkg_resources.working_set.by_key.keys() - for package in packages: - if package.lower().replace("-", "_") == name: - return True - return False + try: + importlib_metadata.distribution(name) + return True + except: # noqa: E722 + return False def get_package_path(name):