运行Spacy代码遇OSError[E050]:找不到en_core_web_sm模型如何解决?
Spacy加载'en_core_web_sm'模型失败的解决方法
运行以下Spacy代码时,出现无法找到'en_core_web_sm'模型的错误:
# load the model nlp = spacy.load('en_core_web_sm') # check the same string with spaCy string = "Our Deeds are the Reason of this #earthquake May ALLAH Forgive us all" print([(token.text, token.pos_) for token in nlp(string) if token.pos_=='PROPN'])
错误信息如下:
OSError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_17348\4216200013.py in <module> 1 # load the model ----> 2 nlp = spacy.load('en_core_web_sm') 3 4 # check the same string with spaCy 5 string = "Our Deeds are the Reason of this #earthquake May ALLAH Forgive us all" ~\anaconda3\lib\site-packages\spacy\__init__.py in load(name, vocab, disable, enable, exclude, config) 52 RETURNS (Language): The loaded nlp object. 53 """ ---> 54 return util.load_model( 55 name, 56 vocab=vocab, ~\anaconda3\lib\site-packages\spacy\util.py in load_model(name, vocab, disable, enable, exclude, config) 437 if name in OLD_MODEL_SHORTCUTS: 438 raise IOError(Errors.E941.format(name=name, full=OLD_MODEL_SHORTCUTS[name])) # type: ignore[index] ---> 439 raise IOError(Errors.E050.format(name=name)) 440 441 OSError: [E050] Can't find model 'en_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.
解决方法
- 使用pip直接安装模型:
若使用conda环境,可改用:pip install en-core-web-smconda install -c conda-forge spacy-model-en_core_web_sm - 通过spacy的内置命令下载模型:
python -m spacy download en_core_web_sm - 若已手动下载模型文件,可直接指定路径加载:
nlp = spacy.load("/path/to/en_core_web_sm")
内容的提问来源于stack exchange,提问作者Rucha Kumthekar
相关产品推荐
相关产品推荐

