首页 > 解决方案 > OSerror 导入语言模型 spacy

问题描述

我正在尝试使用 spacy 。我需要下载英语、意大利语和西班牙语的语言模型。我不能手动安装模型(因为我希望构建一段可移植的代码)所以我写了一个小函数,基本上是

import os
import spacy    
lang='en'
try:
    mod = lang+'_core_web_sm'
    nlp = spacy.load(mod)
except:
    print('model not present.. downloading and loading')
    cmd = 'python -m spacy download '+ mod
    os.system(cmd)
    nlp = spacy.load(mod)

我在一个带有pippython3、windows 10 的 virtualenv 中。

模型下载很好。这是 os.system(cmd) 的输出

从https://github.com/explosion/spacy-models/releases/download/it_core_news_sm-2.2.5/it_core_news_sm-2.2.5.tar.gz#egg=it_core_news_sm==2.2.5收集 it_core_news_sm==2.2.5 下载 https://github.com/explosion/spacy-models/releases/download/it_core_news_sm-2.2.5/it_core_news_sm-2.2.5.tar.gz fumagalli\classifybusiness\lib\site-packages (from spacy>=2.2.2->it_core_news_sm==2.2.5) (1.16.4) 要求已经满足:importlib-metadata>=0.20; c:\users\marco.fumagalli\classifybusiness\lib\site-packages 中的 python_version < "3.8" (来自目录<1.1.0,>=0.0.7->spacy>=2.2.2->it_core_news_sm==2.2。 5) (1.5.0) 要求已经满足:c:\users\marco.fumagalli\classifybusiness\lib\site-packages 中的 urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (从 requests<3.0.0,>=2.13.0->spacy>=2.2.2->it_core_news_sm==2.2.5) (1.23) 要求已经满足:chardet<4,>=3.0.2 in c:\users \marco.fumagalli\classifybusiness\lib\site-packages (来自 requests<3.0.0,>=2.13.0->spacy>=2.2.2->it_core_news_sm==2.2.5) (3.0.4) 要求已经满足: 证书>

但是何时spacy.load(mod)执行:

OSError:[E050] 找不到模型“en_core_web_sm”。它似乎不是快捷链接、Python 包或数据目录的有效路径。

很奇怪,因为如果我这样做

import en_core_web_sm
en_core_web_sm.load()

有用。

我该如何解决?

谢谢

标签: pythonspacy

解决方案


尝试通过以下方式安装 en_core_web_sm:

pip3 install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.2.5/en_core_web_sm-2.2.5.tar.gz

推荐阅读