Merge pull request #5495 from explosion/fix/simplify-is-package

This commit is contained in:
Ines Montani 2020-05-24 15:42:55 +02:00 committed by GitHub
commit cf156ed2f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 27 deletions

View File

@ -5,6 +5,7 @@ import sys
from wasabi import msg from wasabi import msg
from .. import about from .. import about
from ..util import is_package
def download( def download(
@ -17,7 +18,7 @@ def download(
flag is set, the command expects the full model name with version. flag is set, the command expects the full model name with version.
For direct downloads, the compatibility check will be skipped. 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( msg.warn(
"Skipping model package dependencies and setting `--no-deps`. " "Skipping model package dependencies and setting `--no-deps`. "
"You don't seem to have the spaCy package itself installed " "You don't seem to have the spaCy package itself installed "
@ -45,21 +46,6 @@ def download(
"Download and installation successful", "Download and installation successful",
f"You can now load the model via spacy.load('{model_name}')", 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): def get_json(url, desc):

View File

@ -26,10 +26,12 @@ def test_util_ensure_path_succeeds(text):
assert isinstance(path, Path) assert isinstance(path, Path)
@pytest.mark.parametrize("package", ["numpy"]) @pytest.mark.parametrize(
def test_util_is_package(package): "package,result", [("numpy", True), ("sfkodskfosdkfpsdpofkspdof", False)]
)
def test_util_is_package(package, result):
"""Test that an installed package via pip is recognised by util.is_package.""" """Test that an installed package via pip is recognised by util.is_package."""
assert util.is_package(package) assert util.is_package(package) is result
@pytest.mark.parametrize("package", ["thinc"]) @pytest.mark.parametrize("package", ["thinc"])

View File

@ -341,13 +341,10 @@ def is_package(name):
name (unicode): Name of package. name (unicode): Name of package.
RETURNS (bool): True if installed package, False if not. RETURNS (bool): True if installed package, False if not.
""" """
import pkg_resources try:
importlib_metadata.distribution(name)
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 True
except: # noqa: E722
return False return False