ImportError: No module named 'spacy.en'

匿名 (未验证) 提交于 2019-12-03 02:15:02

问题:

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

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.

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.

回答1:

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.lang to keep thing cleaner and better organised. So instead of using spacy.en, you now import from spacy.lang.en.

English. spaCy will do this for you.

nlp = spacy.load('en')  # <-- an instance of `English` with data loaded in doc = nlp(u"This is a text.") 

Basically, spacy.load('en') is a shortcut for:

  1. Find the installed model named 'en' (a package or shortcut link).
  2. Read its meta.json and check which language it's using, and how its processing pipeline should look.
  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.

See this section in the docs for more details.



回答2:

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

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


回答3:

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



回答4:

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

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!