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