mirror of https://github.com/explosion/spaCy.git
Simplify is_package check
This commit is contained in:
parent
15d3a0ac3a
commit
f9786d765e
|
@ -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):
|
||||||
|
|
|
@ -341,14 +341,11 @@ 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
|
return True
|
||||||
packages = pkg_resources.working_set.by_key.keys()
|
except: # noqa: E722
|
||||||
for package in packages:
|
return False
|
||||||
if package.lower().replace("-", "_") == name:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def get_package_path(name):
|
def get_package_path(name):
|
||||||
|
|
Loading…
Reference in New Issue