mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-25 09:26:27 +03:00
Merge pull request #5495 from explosion/fix/simplify-is-package
This commit is contained in:
commit
cf156ed2f4
|
@ -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):
|
||||
|
|
|
@ -26,10 +26,12 @@ def test_util_ensure_path_succeeds(text):
|
|||
assert isinstance(path, Path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("package", ["numpy"])
|
||||
def test_util_is_package(package):
|
||||
@pytest.mark.parametrize(
|
||||
"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."""
|
||||
assert util.is_package(package)
|
||||
assert util.is_package(package) is result
|
||||
|
||||
|
||||
@pytest.mark.parametrize("package", ["thinc"])
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user