一、现象
运行命令:streamlit run promptsource/app.py 报错:
streamlit run promptsource/app.py Traceback (most recent call last): File "/usr/local/bin/streamlit", line 5, in <module> from streamlit.cli import main File "/usr/local/lib/python3.10/dist-packages/streamlit/__init__.py", line 49, in <module> from streamlit.proto.RootContainer_pb2 import RootContainer File "/usr/local/lib/python3.10/dist-packages/streamlit/proto/RootContainer_pb2.py", line 33, in <module> _descriptor.EnumValueDescriptor( File "/usr/local/lib/python3.10/dist-packages/google/protobuf/descriptor.py", line 920, in __new__ _message.Message._CheckCalledFromGeneratedFile() TypeError: Descriptors cannot be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are: 1. Downgrade the protobuf package to 3.20.x or lower. 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
二、以下是针对该问题的分步解决方案:
问题原因分析:
该错误源于Protobuf库版本与Streamlit依赖不兼容,通常发生在Protobuf 4.x版本中。以下是三种解决方法:
方法1:降级Protobuf版本(推荐)
# 查看当前protobuf版本 pip show protobuf# 降级到兼容版本 pip install protobuf==3.20.3# 清理安装缓存 pip cache purge# 重新运行应用 streamlit run promptsource/app.py
方法2:设置环境变量(临时方案)
# 运行前添加环境变量(速度会变慢) PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python streamlit run promptsource/app.py
方法3:创建纯净虚拟环境
# 创建并激活虚拟环境(Python 3.7) conda create -n promptsource_env python=3.7 conda activate promptsource_env# 安装依赖 pip install promptsource pip install protobuf==3.20.3# 运行应用 streamlit run promptsource/app.py
附加说明:
-
如果使用Jupyter环境,需重启kernel使版本更改生效
-
完整依赖冲突检测命令:
pip check
-
项目要求Python 3.7的解决方案:
# 使用pyenv管理多版本 pyenv install 3.7.13 pyenv local 3.7.13