Search code examples
pythonpython-3.xspacy

error while importing Matcher from spacy.matcher


I am trying to use spacy in order to get proper names from a text, but when I run the code I get

Traceback (most recent call last):
  File "C:/Users/L/Desktop/spacy.py", line 2, in <module>
    import spacy
  File "C:\Users\L\Desktop\spacy.py", line 3, in <module>
    from spacy.matcher import Matcher
ModuleNotFoundError: No module named 'spacy.matcher'; 'spacy' is not a package

I installed spacy and its trained model with pip using

pip install spacy
python -m spacy download en_core_web_sm 

Here is the code I am working on:


import spacy
from spacy.matcher import Matcher


# load pre-trained model
nlp = spacy.load('en_core_web_sm')

# initialize matcher with a vocab
matcher = Matcher(nlp.vocab)


def extract_name(resume_text):
    nlp_text = nlp(resume_text)

    # First name and Last name are always Proper Nouns
    pattern = [{'POS': 'PROPN'}, {'POS': 'PROPN'}]

    matcher.add('NAME', None, *pattern)

    matches = matcher(nlp_text)

    for match_id, start, end in matches:
        span = nlp_text[start:end]
        return span.text


text ="some text"

fullName = extract_name(text)

Can anyone help with this error?


Solution

  • The problem is that you called your file spacy.py, while you're also trying to use the package spacy. Never name your script the same as an existing module.

    Solution: rename your file to something different (e.g. "main.py" or "app.py").