Skip to main content

How to Solve "ModuleNotFoundError: No module named 'spacy'" in Python

The ModuleNotFoundError: No module named 'spacy' error indicates that the spaCy Natural Language Processing (NLP) library is not installed in your current Python environment, or that Python can not locate it.

This guide will walk you through:

  • Installing spaCy correctly.
  • Downloading a language model (essential for using spaCy).
  • Troubleshooting common installation problems.
  • Configuring your IDE (VS Code, PyCharm) and Jupyter.

Installing spaCy and a Language Model (The Basic Solution)

Installing spaCy requires two steps:

  • Step 1: Install the spacy library:

    pip install -U spacy
    • -U: This flag ensures you get the latest version (upgrade if already installed). It's good practice to include this.
    • If you have multiple python versions, make sure to install the package using a specific python version.
       pip3 install -U spacy
  • Step 2: Download a language model:

    spaCy uses statistical models for different languages. You must download at least one model before you can use spaCy's core functionality. en_core_web_sm is a good small English model for general use:

    python -m spacy download en_core_web_sm
    • python -m spacy download ...: This is the recommended way to download models. It ensures the model is compatible with your installed spaCy version.
    • en_core_web_sm: This is a small English language model. Other common models include:
      • en_core_web_md (medium English model)
      • en_core_web_lg (large English model)
      • xx_ent_wiki_sm (multi-language model)
      • Models for many other languages (see spaCy's documentation)
    note

    You must download a language model before you can use spaCy. The model provides the vocabulary, trained weights, and other data that spaCy needs to process text.

Verifying the Installation

After installing and downloading a model, test your setup:

import spacy

nlp = spacy.load("en_core_web_sm") # Load the model
doc = nlp("This is a sentence.") # Process some text
print(doc) # Output: This is a sentence.

If this code runs without errors, spaCy is installed and working correctly. If you get a ModuleNotFoundError or an error about the model not being found, proceed to troubleshooting.

Troubleshooting ModuleNotFoundError

Virtual Environments (Essential)

Always use virtual environments:

  1. Create: python3 -m venv venv (or python -m venv venv, or py -m venv venv)
  2. Activate:
    • Linux/macOS: source venv/bin/activate
    • Windows (cmd): venv\Scripts\activate.bat
    • Windows (PowerShell): venv\Scripts\Activate.ps1
      • If PowerShell throws an error, you might need to enable script execution using: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  3. Install: pip install -U spacy and python -m spacy download en_core_web_sm (inside the activated environment).

Your terminal prompt will change (e.g., (venv) $).

Multiple Python Versions:

  • Check: python --version
  • Correct pip: Use pip, pip3, pip3.x, or python -m pip to match your intended Python version.

IDE Configuration (VS Code, PyCharm)

Ensure your IDE is using the correct interpreter/environment.

Jupyter Notebook

!pip install -U spacy
!python -m spacy download en_core_web_sm
note

Make sure your Jupyter kernel uses the correct environment.

Naming Conflicts

Never name your files spacy.py. This will conflict with the library.

Reinstalling spaCy

If nothing else works, try:

pip uninstall spacy
pip install -U spacy
python -m spacy download en_core_web_sm

Installation Instructions for Specific Environments

Windows:

  • Open Command Prompt or PowerShell.
  • Use pip install -U spacy (or py -m pip install -U spacy).
  • Download a model: python -m spacy download en_core_web_sm

macOS / Linux:

  • Open your Terminal.
  • Use pip3 install -U spacy
  • Download a model: python3 -m spacy download en_core_web_sm

Anaconda:

  • Use conda install -c conda-forge spacy
  • Download a model: python -m spacy download en_core_web_sm

Jupyter Notebook (within a cell):

!pip install -U spacy
!python -m spacy download en_core_web_sm

Handling "Import 'spaCy' could not be resolved from source Pylance" (VS Code)

  • Check interpreter: Python: Select Interpreter.
  • Restart VS Code.
  • Last resort (not recommended): import spacy # type: ignore

Conclusion

The ModuleNotFoundError: No module named 'spacy' is almost always due to installation issues or environment misconfiguration.

  • Always use virtual environments.
  • Double-check your Python version and IDE settings.
  • Remember that installing spaCy requires downloading a language model.

This guide provided comprehensive steps and addressed common problems. By following these guidelines, you can confidently use spaCy for your NLP tasks.