Chromebook环境下Streamlit插入图片失败技术求助
问题:Streamlit加载图片提示“无此文件或目录”
我在Chromebook上使用Python的Streamlit框架搭建个人网站,插入图片时终端提示“无此文件或目录”。已在代码同级目录创建images文件夹并放入图片,但问题仍未解决。
我的代码
from PIL import Image import requests import streamlit as st from streamlit_lottie import st_lottie # 更多表情可参考相关链接,第4行设置网站用户端布局 st.set_page_config(page_title="My Webpage", page_icon=":tada:", layout="wide") def load_lottieurl(url): r = requests.get(url) if r.status_code != 200: return None return r.json() #Lottie用于添加网站动画(需安装requests、streamlit-lottie、pillow) lottie_coding = load_lottieurl("https://assets7.lottiefiles.com/packages/lf20_2znxgjyt.json") img_william = Image.open("images\\william.png") img_tacos = Image.open("images\\tacos.png") # 头部区域,st.container用于整理代码,不使用也能运行 with st.container(): st.subheader("hello i am william and this is my first website made by python header :wave:") st.title("this would be were the title would go ") st.write("this would be a small paragraph you wold write this area") # st.write用于短段落,st.title用于段落标题 # 此处的短横线用于分隔网站空间 with st.container(): st.write("---") left_column, right_column = st.columns(2) with left_column: st.header("this will be the column for the left side ") st.write('##') st.write( """ i am currently doing what the tutotial is telling me to do even though i am quite cofnused: - i will master this and i will understtand the concepts of making a website with this framework - i will be great i bless god for changing how i act and i am as a person - i know allot of people do not want to see me win but god does and thats all i need - rome was not built in a day and they will say the samething about my journey i am confused but if i keep puttingand effort in everyday i will master this in jesues name """ )
解决方案
核心原因:路径分隔符不兼容
Chromebook基于Linux系统,路径分隔符使用斜杠/,但代码中误用了Windows系统的反斜杠\\,导致系统无法定位文件。
修复步骤:
修改路径分隔符
将代码中的图片路径从"images\\william.png"改为"images/william.png","images\\tacos.png"改为"images/tacos.png"。修改后的代码片段:img_william = Image.open("images/william.png") img_tacos = Image.open("images/tacos.png")确认文件结构
确保你的项目文件结构如下:项目根目录/ ├── 你的代码文件.py └── images/ ├── william.png └── tacos.png检查Chromebook文件权限(可选)
如果修改路径后仍报错,检查images文件夹及图片的读取权限:- 打开终端,进入项目根目录,执行:
ls -l images/ - 若文件权限不含
r(可读),执行:chmod +r images/*
- 打开终端,进入项目根目录,执行:
绝对路径调试(可选)
若相对路径仍有问题,可通过打印当前工作目录验证文件位置:import os print(os.getcwd()) # 打印代码运行的当前目录 print(os.listdir("images")) # 打印images文件夹内的文件列表根据终端输出确认图片是否存在于指定目录中。
内容的提问来源于stack exchange,提问作者Glitch
相关产品推荐
相关产品推荐

