树莓派4开机自启Python应用出现JSONDecodeError问题求助
树莓派LXDE开机自启Python应用JSON解析错误解决方案
核心问题定位
报错json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)的本质是config.json文件为空或内容损坏,导致JSON解析失败。手动运行正常但开机自启异常,主要原因是工作目录不一致或文件写入不完整。
修复方案
1. 强制使用绝对路径读写配置文件
开机自启时脚本的工作目录默认不是应用所在目录,相对路径会导致读写错误(比如在根目录创建空的config.json)。修改代码为绝对路径:
# 在CameraParameters.py顶部定义配置文件绝对路径 CONFIG_FILE = "/home/diego/Desktop/.MVP1.0/config.json" def save_configuration(self): config = { "brightness": self.brightness_slider.value(), "contrast": self.contrast_slider.value(), "saturation": self.saturation_slider.value(), "sharpness": self.sharpness_slider.value(), "exposure": self.exposure_slider.value(), "colourgainsR": self.colourgainsR_slider.value(), "colourgainsB": self.colourgainsB_slider.value(), "enable_WB": self.enable_WB_checkbox.isChecked(), "white_balance": self.white_balance_combobox.currentIndex(), "enable_AE": self.enable_checkbox.isChecked(), "enable_crosshairs": self.enable_crosshairs_checkbox.isChecked(), "crosshairs_color": self.lineColor_combobox.currentText(), "crosshairs": { "crosshairs_color_R": self.red, "crosshairs_color_G": self.green, "crosshairs_color_B": self.blue, }, "vflip_state": self.vflip_state, "hflip_state": self.hflip_state } # 使用绝对路径保存 with open(CONFIG_FILE, "w") as config_file: json.dump(config, config_file) def load_configuration(self): try: with open(CONFIG_FILE, "r") as config_file: config = json.load(config_file) self.brightness_slider.setValue(config["brightness"]) self.contrast_slider.setValue(config["contrast"]) self.saturation_slider.setValue(config["saturation"]) self.sharpness_slider.setValue(config["sharpness"]) self.exposure_slider.setValue(config["exposure"]) self.colourgainsR_slider.setValue(config["colourgainsR"]) self.colourgainsB_slider.setValue(config["colourgainsB"]) self.enable_WB_checkbox.setChecked(config["enable_WB"]) self.white_balance_combobox.setCurrentIndex(config["white_balance"]) self.enable_checkbox.setChecked(config["enable_AE"]) self.enable_crosshairs_checkbox.setChecked(config["enable_crosshairs"]) self.lineColor_combobox.setCurrentText(config["crosshairs_color"]) self.red = config["crosshairs"]["crosshairs_color_R"] self.green = config["crosshairs"]["crosshairs_color_G"] self.blue = config["crosshairs"]["crosshairs_color_B"] self.vflip_state = config["vflip_state"] self.hflip_state = config["hflip_state"] except FileNotFoundError: # 文件不存在时自动生成默认配置 self.save_configuration() except json.JSONDecodeError: # 配置损坏时重置为默认 print("配置文件损坏,重置为默认设置") self.save_configuration()
2. 原子性写入避免文件损坏
异常退出时(如强制关机),直接写入可能导致文件不完整。改用临时文件替换的方式保证写入原子性:
def save_configuration(self): config = { # ... 原配置内容不变 } temp_file = "/home/diego/Desktop/.MVP1.0/config.tmp" # 先写入临时文件 with open(temp_file, "w") as f: json.dump(config, f) # 替换原文件,保证操作原子性 import os os.replace(temp_file, CONFIG_FILE)
3. 修正开机自启命令的工作目录
如果通过~/.config/lxsession/LXDE-pi/autostart添加自启项,修改命令为:
@cd /home/diego/Desktop/.MVP1.0 && python3 entry_point.py
或创建shell脚本包装执行:
创建start_app.sh:
#!/bin/bash cd /home/diego/Desktop/.MVP1.0 python3 entry_point.py
赋予执行权限:
chmod +x /home/diego/Desktop/.MVP1.0/start_app.sh
然后在autostart中添加:
@/home/diego/Desktop/.MVP1.0/start_app.sh
4. 临时应急方案
如果需要快速恢复运行,仅修改load_configuration的异常捕获逻辑,保证即使配置损坏也能正常启动:
def load_configuration(self): try: with open(CONFIG_FILE, "r") as config_file: config = json.load(config_file) # ... 原加载逻辑 except (FileNotFoundError, json.JSONDecodeError): # 任何配置问题都重置为默认 self.save_configuration()
验证步骤
- 手动删除或清空
config.json,运行应用确认自动生成正确配置 - 重启系统,检查应用开机自启是否正常,
config.json内容完整
内容的提问来源于stack exchange,提问作者Diego Sanchez
相关产品推荐
相关产品推荐

