ImportError: cannot import name 'xxx' after upgrading Streamlit
0 reputation · 14 May 2023, 09:38 UTC
0 reputation · 14 May 2023, 09:38 UTC
The goal is to restore a stable development environment after a pip upgrade of Streamlit that leaves the application unable to start due to an ImportError such as cannot import name 'xxx'. This typically indicates a binary incompatibility between the new Streamlit wheel and its compiled dependencies.
Current constraints include the lack of a built‑in rollback feature in Streamlit, the need to preserve other package versions that may depend on the upgraded API, and the difficulty of detecting the failure before the app is launched.
Unresolved questions:
32275 reputation · 14 May 2023, 14:19 UTC
When you encounter an ImportError: cannot import name 'xxx' immediately after upgrading Streamlit, it is usually because the new version has removed, renamed, or relocated that specific function or class. This is frequently seen during major version jumps where internal APIs are refactored or deprecated features are finally stripped out.
The most likely cause is a breaking change in the Streamlit API. For example, older versions of Streamlit used st.cache, now replaced by st.cache_data and st.cache_resource. If your code or a third-party dependency relies on the old name, the upgrade will cause the import to fail because the symbol no longer exists in the module namespace.
To recover your environment and fix the error, follow these steps:
pip show streamlitpip install streamlit==[previous_version_number]xxx. Check the Streamlit release notes for that specific version jump to find the replacement.To provide a specific code replacement for xxx, what is the exact name of the failing import and which Streamlit version are you currently running?
Use comments to ask for clarification. Post a solution as an answer.
32,275 reputation · 14 May 2023, 14:09 UTC
Streamlit’s wheels ship compiled extensions and internal modules that sometimes move between minor releases. If a previous __pycache__ entry still references a removed module, Python will raise ImportError: cannot import name 'xxx' even though the source code is correct.
# Remove all cached bytecode
find . -name "*.pyc" -delete
find . -name "__pycache__" -type d -exec rm -rf {} +
# Re‑run the app to let Python regenerate the cache
python app.py
Alternatively, a forced reinstall that skips dependency checks keeps the environment intact:
pip install --force-reinstall --no-deps streamlit==1.38.0
Both steps avoid a full downgrade and preserve other package versions.