Python 导入错误:没有名为“spacy.en”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47295316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:07:43  来源:igfitidea点击:

ImportError: No module named 'spacy.en'

pythonspacy

提问by rayabhik

I'm working on a codebase that uses Spacy. I installed spacy using:

我正在研究使用 Spacy 的代码库。我使用以下方法安装了 spacy:

sudo pip3 install spacy

and then

进而

sudo python3 -m spacy download en

At the end of this last command, I got a message:

在最后一条命令结束时,我收到一条消息:

    Linking successful
/home/rayabhik/.local/lib/python3.5/site-packages/en_core_web_sm -->
/home/rayabhik/.local/lib/python3.5/site-packages/spacy/data/en

You can now load the model via spacy.load('en')

Now, when I try running my code, on the line:

现在,当我尝试运行我的代码时,就行了:

    from spacy.en import English

it gives me the following error:

它给了我以下错误:

ImportError: No module named 'spacy.en'

I've looked on Stackexchange and the closest is: Import error with spacy: "No module named en"which does not solve my problem.

我看过 Stackexchange,最接近的是: 导入错误与 spacy:“没有名为 en 的模块”这不能解决我的问题。

Any help would be appreciated. Thanks.

任何帮助,将不胜感激。谢谢。

Edit: I might have solved this by doing the following:

编辑:我可能已经通过执行以下操作解决了这个问题:

 Python 3.5.2 (default, Sep 14 2017, 22:51:06) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import spacy
>>> spacy.load('en')
<spacy.lang.en.English object at 0x7ff414e1e0b8>

and then using:

然后使用:

from spacy.lang.en import English

I'm still keeping this open in case there are any other answers.

如果有任何其他答案,我仍然保持开放状态。

采纳答案by Ines Montani

Yes, I can confirm that your solution is correct. The version of spaCy you downloaded from pip is v2.0, which includes a lot of new features, but also a few changes to the API. One of them is that all language data has been moved to a submodule spacy.langto keep thing cleaner and better organised. So instead of using spacy.en, you now import from spacy.lang.en.

是的,我可以确认您的解决方案是正确的。你从pip下载的spaCy版本是v2.0,它包含了很多新功能,但也有一些API的变化。其中之一是所有语言数据都已移动到子模块中,spacy.lang以保持更清洁和更好的组织。因此spacy.en,您现在从 导入,而不是使用spacy.lang.en

- from spacy.en import English
+ from spacy.lang.en import English

However, it's also worth mentioning that what you download when you run spacy download enis not the same as spacy.lang.en. The language data shipped with spaCy includes the static data like tokenization rules, stop words or lemmatization tables. The enpackage that you can download is a shortcut for the statistical model en_core_web_sm. It includes the language data, as well as binary weight to enable spaCy to make predictions for part-of-speech tags, dependencies and named entities.

但是,还值得一提的是,您运行时下载spacy download en的内容与spacy.lang.en. spaCy 附带的语言数据包括静态数据,如标记规则、停用词或词形还原表。en您可以下载的包是统计模型的快捷方式en_core_web_sm。它包括语言数据以及二进制权重,使 spaCy 能够对词性标签、依赖项和命名实体进行预测。

Instead of just downloading en, I'd actually recommend using the full model name, which makes it much more obvious what's going on:

而不是仅仅下载en,我实际上建议使用完整的模型名称,这使得正在发生的事情更加明显:

python -m spacy download en_core_web_sm
nlp = spacy.load("en_core_web_sm")

When you call spacy.load, spaCy does the following:

当您调用 时spacy.load,spaCy 会执行以下操作:

  1. Find the installed model named "en_core_web_sm"(a package or shortcut link).
  2. Read its meta.jsonand check which language it's using (in this case, spacy.lang.en), and how its processing pipeline should look (in this case, tagger, parserand ner).
  3. Initialise the language class and add the pipeline to it.
  4. Load in the binary weights from the model data so pipeline components (like the tagger, parser or entity recognizer) can make predictions.
  1. 找到名为"en_core_web_sm"(包或快捷链接)的已安装模型。
  2. 阅读它meta.json并检查它使用的是哪种语言(在本例中为spacy.lang.en),以及其处理管道的外观(在本例中为taggerparserner)。
  3. 初始化语言类并向其添加管道。
  4. 从模型数据加载二进制权重,以便管道组件(如标记器、解析器或实体识别器)可以进行预测。

See this section in the docsfor more details.

有关更多详细信息,请参阅文档中的这一部分

回答by anees ahmed

I used the following command for installing spacy from anaconda distribution.

我使用以下命令从 anaconda 发行版安装 spacy。

conda install -c conda-forge spacy

and after that, I was able to download English using the following command without any error.

之后,我可以使用以下命令下载英语,没有任何错误。

 python -m spacy download en

回答by Elham

I had to use en_core_web_sminstead of ento make that work. It is complaining about permission problem. The following works perfectly:

我不得不使用en_core_web_sm而不是en来完成这项工作。它正在抱怨权限问题。以下工作完美:

import spacy
spacy.load('en_core_web_sm')
from spacy.lang.en import English

回答by gdaras

I think that there is a confusion in the answers provided. Correct things mentioned:

我认为所提供的答案存在混淆。提到的正确的事情:

  • you should import from spacy.lang.en
  • spacy.load('en') is indeed a shortcut for loading models.
  • 你应该从 spacy.lang.en 导入
  • spacy.load('en') 确实是加载模型的快捷方式。

But:the file en_core_web_sm is not the same file as the one you import from spacy.lang.en. Actually, the first file is produced from the second after training with spacy train in a dataset and then packaging the result. spacy.lang.en contains the model definition: lemmas lookup table, stop_words, lexical attributes (and more). But that and only that. It is not trained with a dataset so that the dependency graph and other functionalities can work.

但是:文件 en_core_web_sm 与您从 spacy.lang.en 导入的文件不同。实际上,第一个文件是在数据集中用 spacy train 训练后从第二个文件生成的,然后打包结果。spacy.lang.en 包含模型定义:引理查找表、stop_words、词法属性(等等)。但仅此而已。它没有使用数据集进行训练,因此依赖图和其他功能可以工作。

I think this should be clear enough when working with spaCy.

我认为在使用 spaCy 时这应该足够清楚了。

回答by Sujay DSa

the en_core_web_sm folder was downloaded outside the spacy folder. I copied it into the spacy/data folder and could run the code as documented in spacy

en_core_web_sm 文件夹被下载到 spacy 文件夹之外。我将它复制到 spacy/data 文件夹中,并且可以运行 spacy 中记录的代码

回答by fatcook

Anyone facing this issue on Windows 10 and Anaconda installation , look for your conda python executable using where pythonon command line before running the script.

在 Windows 10 和 Anaconda 安装上遇到此问题的任何人,请where python在运行脚本之前在命令行上查找您的 conda python 可执行文件。

In my case, the python on the PATH was

就我而言,PATH 上的 python 是

C:\Users\XXX\.windows-build-tools\python27\python.exe

C:\Users\XXX\.windows-build-tools\python27\python.exe

whereas what I needed was from

而我需要的是来自

c:\Users\XXX\AppData\Local\Continuum\anaconda3\python.exe

c:\Users\XXX\AppData\Local\Continuum\anaconda3\python.exe

Just add the correct python on the path , or go to this location and run

只需在路径上添加正确的python,或转到此位置并运行

python -m spacy download en

python -m spacy download en

and it should work.

它应该工作。

回答by Wallace

According to the official website you should do as follow:

根据官方网站,您应该执行以下操作:

python -m spacy download en

However, this surprisingly does NOT work for me.
As you may interst, my env is based on OSX 10.15 with python 3.8, pip 19.3.1
try:

然而,这出乎意料地对我不起作用。
您可能会感兴趣,我的环境基于 OSX 10.15 和 python 3.8,pip 19.3.1
试试:

spacy download en

回答by gobind agarwal

from spacy.lang.en import English instead of from spacy.en import English

from spacy.lang.en import English 而不是 from spacy.en import English

回答by msh855

For me what worked are these steps:

对我来说,这些步骤有效:

import sys
!{sys.executable} -m pip install spacy
!{sys.executable} -m spacy download en

I run these steps at my spyder console (installed through anaconda)

我在我的 spyder 控制台上运行这些步骤(通过 anaconda 安装)