Pylance提示import "streamlit_webrtc"无法解析,Streamlit运行遇阻求助
streamlit_webrtc组件启动报错,常规重装无法解决
问题复现
执行streamlit run app.py启动Streamlit服务器时,Streamlit本体运行正常,但streamlit_webrtc组件报错,已尝试卸载重装Python、重新安装streamlit及streamlit-webrtc、搭建虚拟环境并更换多款IDE,问题仍未解决。
可行排查/解决步骤
- 确认Python版本兼容性:streamlit-webrtc仅支持Python 3.8~3.11版本,执行
python --version检查版本是否在范围内,不在则更换对应版本。 - 安装指定稳定版本的streamlit-webrtc:最新版本可能存在兼容bug,尝试安装经过验证的稳定版:
pip install streamlit-webrtc==0.45.0 - 手动修复依赖库:streamlit-webrtc依赖aiortc、pyav等库,版本不匹配会导致报错,强制安装兼容版本:
pip install aiortc==1.4.0 pyav==10.0.0 - 清理缓存后彻底重装:清理pip缓存避免残留旧版本文件,再重新安装相关库:
pip cache purge pip uninstall -y streamlit streamlit-webrtc aiortc pyav pip install streamlit streamlit-webrtc - 补充系统级依赖(Linux/macOS):
- Ubuntu/Debian系统:
sudo apt-get install libavdevice-dev libavfilter-dev libopus-dev libvpx-dev - macOS系统(需先安装Homebrew):
brew install ffmpeg opus libvpx
- Ubuntu/Debian系统:
- 验证最小示例:创建极简测试脚本
test_webrtc.py,排除业务代码干扰:
执行import streamlit as st from streamlit_webrtc import webrtc_streamer webrtc_streamer(key="test")streamlit run test_webrtc.py,若仍报错则聚焦组件与环境兼容问题,否则排查业务代码中的冲突。
内容的提问来源于stack exchange,提问作者Ananda Chaudhary
相关产品推荐
相关产品推荐

