You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Streamlit项目遇UnicodeDecodeError错误,求解决方案

Streamlit运行报错UnicodeDecodeError解决方案

问题背景

我编写了名为app.py的Python文件用于构建项目推荐系统,代码如下:

from PIL import Image
import requests
from os import path
import streamlit as st
from streamlit_lottie import st_lottie

st.set_page_config(
    page_title="Project Recommender System",
    page_icon=":tada:",
    layout="wide",
)

# --function to access json data from animation file --
def load_lottieurl(url):
    r=requests.get(url)
    if r.status_code!=200:
        return None
    return r.json()


lottie_coding="https://lottie.host/?file=27f9afaf-6b4d-4b82-9ddd-0090383cc539/uiGM12Tq9s.json"
img_contact_form = Image.open("images/project-contact-form.png")
img_lottie_animation=Image.open("images/stock-price-prediction-web-app.png")

with st.container():
    st.subheader("AcademiaLink's Project Recommender System")
    st.title("A recommender system for projects of your choice")

# -- Opens Home page --
# st.write("[Explore AcademiaLink >]()")

with right_column:
    st_lottie(lottie_coding, height=300, key="coding")
    
    
with st.container():
    st.header("Project Recommendations")
    st.write("---")
    image_col, text_col=st.columns((1,2))
    with image_col:
        st.image(img_lottie_animation)
    with text_col:
        st.subheader("Sales Forecasting using ARIMA")
        st.write(
    """This is a machine learning project which performs exploratory data analysis on sales forecasting using ARIMA. The dataset used in the project is acquired from Kaggle and since the data proves to be seasonal, we use SARIMAX class model to derive apt and accurate predictions for the next 36 months.
    """
        )
        st.markdown("[Explore the project](https://github.com/vindhya1103/Sales-Forecasting-using-ARIMA)")
        
with st.container():
    image_col, text_col = st.columns((1,2))
    with image_col:
        st.image(img_contact_form)
    with text_col:
        st.subheader("")
        st.write()
        st.markdown("[Explore the project]()")

运行报错

执行streamlit run app.py命令时,命令行输出如下报错:

C:\Users\Vindhya Sree\toolswebapp>streamlit run app.py
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\Scripts\streamlit.exe\__main__.py", line 7, in <module>
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\site-packages\streamlit\web\cli.py", line 201, in main_run
    bootstrap.load_config_options(flag_options=kwargs)
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\site-packages\streamlit\web\bootstrap.py", line 367, in load_config_options
    config.get_config_options(force_reparse=True, options_from_flags=options_from_flags)
  File "C:\Users\Vindhya Sree\AppData\Roaming\Python\Python311\site-packages\streamlit\config.py", line 1229, in get_config_options
    file_contents = input.read()
                    ^^^^^^^^^^^^
  File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

已尝试无效操作

  • 以管理员身份运行命令
  • 调整编码设置
  • 修改页面图标
  • 安装tensorflow

有效解决方案

核心问题原因

报错是因为Streamlit读取配置文件时遇到非UTF-8编码的文件,字节0xff是UTF-16的BOM头,大概率是Streamlit配置文件(config.toml)编码错误或文件损坏。

解决步骤

  1. 定位配置文件:执行streamlit config show命令查看默认配置文件路径,一般位于C:\Users\你的用户名\.streamlit\config.toml,同时检查项目根目录下的.streamlit文件夹是否存在配置文件。
  2. 修复编码问题:用记事本打开配置文件,点击「文件」-「另存为」,将编码改为UTF-8后保存。
  3. 重置配置文件:如果配置文件损坏或无法修复,直接删除该文件,Streamlit会自动生成默认配置文件。
  4. 修复代码小问题:代码中with right_column:未定义列,需在对应位置添加列定义:
    with st.container():
        st.subheader("AcademiaLink's Project Recommender System")
        st.title("A recommender system for projects of your choice")
        # 新增列定义
        left_column, right_column = st.columns(2)
    with right_column:
        st_lottie(lottie_coding, height=300, key="coding")
    
  5. 重新运行:执行streamlit run app.py即可正常启动,获得本地Web URL。

内容的提问来源于stack exchange,提问作者Vindhya1103

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 20:09:55