0

I am building a python package and in my main py file, I import Spacy language model like this. If it exist, it will be loaded otherwise it will be downloaded using shell command.

try:
    nlp = spacy.load('en_core_web_md')
except OSError:
    print("Downloading language model for the spaCy POS tagger do not worry, this will only happen once")
    os.system('python -m spacy download en_core_web_md')
nlp = spacy.load('en_core_web_md')

When I install package, it downloads the model but gives error while importing.

You can now load the model via spacy.load('en_core_web_md')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/shahid/Documents/simple_nlp/NlpFactory/__init__.py", line 1, in <module>
    from NlpFactory.NlpFactory import NLPFactory
  File "/home/shahid/Documents/simple_nlp/NlpFactory/NlpFactory.py", line 28, in <module>
    nlp = spacy.load('en_core_web_md')
  File "/home/shahid/anaconda3/envs/eda_test_7/lib/python3.5/site-packages/spacy/__init__.py", line 30, in load
    return util.load_model(name, **overrides)
  File "/home/shahid/anaconda3/envs/eda_test_7/lib/python3.5/site-packages/spacy/util.py", line 175, in load_model
    raise IOError(Errors.E050.format(name=name))
OSError: [E050] Can't find model 'en_core_web_md'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.

So I thought this is a path issue so I tried importing it like this.

nlp = spacy.load(os.path.abspath('en_core_web_md'))

It gave me this error which is quite similar.

You can now load the model via spacy.load('en_core_web_md')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/shahid/Documents/simple_nlp/NlpFactory/__init__.py", line 1, in <module>
    from NlpFactory.NlpFactory import NLPFactory
  File "/home/shahid/Documents/simple_nlp/NlpFactory/NlpFactory.py", line 28, in <module>
    nlp = spacy.load(os.path.abspath('en_core_web_md'))
  File "/home/shahid/anaconda3/envs/eda_test_7/lib/python3.5/site-packages/spacy/__init__.py", line 30, in load
    return util.load_model(name, **overrides)
  File "/home/shahid/anaconda3/envs/eda_test_7/lib/python3.5/site-packages/spacy/util.py", line 175, in load_model
    raise IOError(Errors.E050.format(name=name))
OSError: [E050] Can't find model '/home/shahid/Documents/simple_nlp/en_core_web_md'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.

I have looked at this question about vendoring but it is not recommended so I am not going to adopt it. Package spacy model

shahid hamdam
  • 751
  • 1
  • 10
  • 24
  • These sorts of issues are almost always something to do with an incorrect interpreter. Can you verify they're the same by checking the output of `os.system("which python")` and ensuring that it matches with the output of `print(sys.executable)`? If they aren't the same, try replacing `"python -m spacy..."` with `"{} -m spacy...".format(sys.executable)` – Eric Le Fort Jul 05 '20 at 06:08
  • they are same in my case /home/shahid/anaconda3/envs/eda_test_7/bin/python – shahid hamdam Jul 05 '20 at 06:26
  • Are you certain the model installer completed without error? Also, is it possible there's a permissions issue with the model file? – Eric Le Fort Jul 05 '20 at 18:06

0 Answers0