Streamlit Spaces with "ModuleNotFoundError: No module named 'altair.vegalite.v4'" error

TL;DR

If you encounter the error ModuleNotFoundError: No module named ‘altair.vegalite.v4’ in Streamlit Spaces,
do one of the following to fix it:

  • Add altair<5 to requirements.txt
  • Set the sdk_version in README.md to 1.20.0 or 1.21.0.

Description

As reported in this Streamlit forum post, this error started appearing few days ago.
This bug occurs when using Streamlit 1.19.0 or earlier with Altair v5, and it became apparent with a recent release of Alteir v5.

In Hugging Face Spaces, the default Streamlit version is 1.19.0. So creating a new Streamlit Space or rebuilding an existing one will result in this error.

To fix it, you can either add altair<5 to requirements.txt or set the sdk_version in README.md to 1.20.0 or 1.21.0 (1.22.0 is not yet available on HF Spaces).