Streamlit导入报错:无法从streamlit.type_util导入convert_anything_to_df
解决Streamlit连接谷歌表格时的ImportError问题
问题原因
你遇到的ImportError: cannot import name 'convert_anything_to_df' from 'streamlit.type_util'错误,是因为Streamlit 1.37.0版本移除了内部的convert_anything_to_df函数,而你当前使用的streamlit_gsheets第三方包依赖这个已被废弃的函数。此外,你可能混淆了旧的第三方包和Streamlit官方维护的谷歌表格连接工具。
解决方案
方案一:切换到官方Streamlit谷歌表格连接包
这是推荐的长期解决方案,官方包会持续适配最新的Streamlit版本:
- 卸载旧的第三方包:
pip uninstall streamlit-gsheets -y - 安装官方的
st-gsheets-connection包:pip install st-gsheets-connection - 修改你的代码,替换导入语句:
import streamlit as st import pandas as pd # 替换原有的导入为官方包的路径 from st_gsheets_connection import GSheetsConnection url = "https://docs.google.com/spreadsheets/d/1JDy9md2VZPz4JbYtRPJLs81_3jUK47nx6GYQjgU8qNY/edit?usp=sharing" conn = st.experimental_connection("gsheets", type=GSheetsConnection) data = conn.read(spreadsheet=url, usecols=[0, 1]) st.dataframe(data) - 重新运行代码即可。
方案二:降级Streamlit到兼容版本
如果你暂时不想更换包,可以将Streamlit降级到1.36.0或更早版本,这些版本仍保留convert_anything_to_df函数:
pip install streamlit==1.36.0
降级后直接运行原代码即可。
额外注意事项
确保你的谷歌表格已设置为**"任何人可查看"**权限(通过表格右上角的"共享"按钮调整),否则即使解决了导入错误,也会因权限问题无法读取数据。
内容的提问来源于stack exchange,提问作者Atreya Khose
相关产品推荐
相关产品推荐

