ModuleNotFoundError-没有名为“ en-core-web-sm”的模块

问题描述

我正在尝试部署一个使用en_core_web_sm的应用程序,该应用程序是spacy的子模块。在我的代码中,我有import en_core_web_sm,在本地测试时效果很好。在我的venv中,我运行了pipenv install spacy[en-core-web-sm],它在Pipfile中生成了该文件spacy = {extras = ["en-core-web-sm"],version = "*"}.我尝试了多种将其导入到我的.py文件中的方法,但是一直得到ModuleNotFoundError。 >

我尝试从en-core-web-smen_core_web_smspacy['en_core_web_sm]spacy.lang.en-core-web-sm等众多其他版本中导入。

我也尝试过en_core_web_sm = __import__('en-core-web-sm')en_core_web_sm = __import__('spacy["en-core-web-sm"]'),因为我知道带有虚线的模块在导入时可能会出现问题。

导入此内容的正确方法是什么?谢谢!

解决方法

使用:

import spacy
nlp = spacy.load("en_core_web_sm")

现在,如果您运行例如:

string = "going went gone"
[token.lemma_ for token in nlp(string)] #lemmatization

您将得到:

['go','go','go']