Installing spaCy and loading pipelines

spaCy · Essentials · 8 min

Nothing linguistic happens without a pipeline; choosing the right one sets your accuracy, speed and memory.

spaCy is installed with pip install spacy, but the library alone has no trained models. Trained pipelines are separate Python packages, installed with:

python -m spacy download en_core_web_sm

The naming scheme is language_type_genre_size: en_core_web_sm is English, general-purpose ("core"), trained on web text, small. Sizes trade accuracy against speed and memory:

  • sm: small and fast, no real word vectors
  • md, lg: add word vectors (useful for similarity), bigger downloads
  • trf: transformer-based (RoBERTa for English), most accurate, much slower, ideally with a GPU

nlp = spacy.load("en_core_web_sm") returns a `Language` object, conventionally named nlp. Calling it on text runs the whole pipeline.

spacy.blank("en") creates a pipeline with only the language's tokenizer and rules, no trained components. It's fast and handy for tokenization, rule-based matching or as the starting point for training your own model.

Pipeline packages are versioned against spaCy: a model for 3.8 expects spaCy 3.8.x. python -m spacy validate checks installed packages for compatibility. Pin both in requirements.txt (pipelines can be installed from their release URLs) so deployments don't drift. nlp.meta shows the model's name, version and accuracy figures.

Example

import spacy

nlp = spacy.load("en_core_web_sm")     # after: python -m spacy download en_core_web_sm
print(nlp.meta["name"], nlp.meta["version"])   # core_web_sm 3.8.0
print(nlp.pipe_names)
# ['tok2vec', 'tagger', 'parser', 'attribute_ruler', 'lemmatizer', 'ner']

doc = nlp("Apple is looking at buying a U.K. startup.")
print([t.text for t in doc])
# ['Apple', 'is', 'looking', 'at', 'buying', 'a', 'U.K.', 'startup', '.']

blank = spacy.blank("en")              # tokenizer only, nothing trained
print(blank.pipe_names)                # []
print(len(blank("Fast tokenization only.")))   # 4

try:
    spacy.load("en_core_web_xx")
except OSError as e:
    print("OSError:", "E050" in str(e))  # True: package not installed

Remember

  • Trained pipelines are separate packages: python -m spacy download <name>
  • Names encode language, type, genre and size (sm, md, lg, trf)
  • spacy.load returns the nlp object; spacy.blank gives a tokenizer-only pipeline
  • Keep pipeline and spaCy versions compatible (spacy validate)

Watch out for

  • Expecting pip install spacy alone to make spacy.load('en_core_web_sm') work.

Check yourself

  1. You need accurate entities and have a GPU; speed is secondary. Which English pipeline fits best?
  2. What does `spacy.blank('en')` give you?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: spacy.io/usage/models