How do I load a spacy model?

What I’m trying to do:
I am looking to run a line of code that looks like this:
nlp = spacy.load(“en_core_web_sm”)

The error I’m getting is
OSError: [E050] Can't find model 'en_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.

  • at /home/anvil/.env/lib/python3.10/site-packages/spacy/util.py:439
  • called from /home/anvil/.env/lib/python3.10/site-packages/spacy/__init__.py:54
  • called from script, line 918
  • called from Form1, line 18

What I’ve tried and what’s not working:
I loaded the spacy model myself (latest version). On my device, I would simply run
-python -m spacy download en_core_web_sm

However, I don’t know how to do this within anvil

See here for the last time this happened:

iirc @brooke had to have it installed it manually, and it was for the python 3.7 environment at the time, (your error message referenced 3.10) so it might need to be added manually again?

I did install it manually, in Python 3.10 because that is the Python Beta version which allows me to self-install my packages. Not sure how to proceed from here.

There’s an answer for it on StackOverflow. Tested it on Anvil and it works.

import en_core_web_sm
nlp = en_core_web_sm.load()
1 Like

For anyone later wondering how @nvilvovsky and @divyeshlakhotia got this to work, I am guessing they did this:

and installed the model pipelines directly through pip and the requirements.txt style installation while installing custom packages into anvil.

then you should be able to import it like any other module.

1 Like