Python Script runs fine on RPi until I try creating shortcut - ConfigParser throws "no section" error

47 Views Asked by At

I have a functioning python script that runs fine when I call it via terminal or any Python editor. But when I create a shortcut or bash to open the file it makes it part way through the code until it comes across ConfigParser which is of course using a config file.

I get the following error:

pi@raspberrypi:~ $ ./short.sh
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
this is a linux machine
i should not be here right now
Traceback (most recent call last):
  File "/home/pi/KeymasterGame/keymaster.py", line 46, in <module>
    comport = config1.get('general', 'comport')
  File "/usr/lib/python3.9/configparser.py", line 781, in get
    d = self._unify_values(section, vars)
  File "/usr/lib/python3.9/configparser.py", line 1149, in _unify_values
    raise NoSectionError(section) from None
configparser.NoSectionError: No section: 'general'

Of course, there is a section "general" which does work when being called from terminal

Python Code (just the beginning until the error):

import tkinter
from tkinter import *
import keyboard
import tkinter.font as TkFont
from pyfirmata import Arduino, util
import time
import pygame
from configparser import ConfigParser
import ast
from sys import platform
import sys
import os

#detect operating system
if platform == "linux" or platform == "linux2":
    print("this is a linux machine")
    linux = 'yes'
    import RPi.GPIO as GPIO
    pygame.mixer.init()

    print("i should not be here right now")

elif platform == "darwin":
   print("this is a mac")
elif platform == "win32":
   print("this is a windows machine")
   import pygame
   pygame.mixer.init()



config1 = ConfigParser()

# setup for increasing page count
updatePage = 1
page = "page" + str(updatePage)
root = "root" + str(updatePage)
bgimage = 'bgimage' + str(updatePage)


### these are the config things that should only be setup once
config1.read("config1.ini")
comport = config1.get('general', 'comport')
win_res = config1.get('general','win_res')
hardware = config1.get('general', 'hardware')
pins = config1.get('general', 'pins')
state = config1.get('general', 'state')

Then, here is the beginning of my config (which works otherwise):

[general]
hardware = 
pins = 11,12
state = 0
usb = no
comport = COM9
win_res = 1920x1080

And I was using a .desktop entry earlier but switched it to this bash for now:

#!/bin/bash

python3.9 /home/pi/KeymasterGame/keymaster.py

As you'll notice, the python script begins to run until it hits the ConfigParser actually looking through the config1.ini file. Yet, it does not behave like this when I simply run the python script manually.

1

There are 1 best solutions below

0
toyota Supra On

Edit:

line 46, in comport = config1.get('general', 'comport')

configparser.NoSectionError: No section: 'general'

Does this help if you're adding if/else condition?

config1.read("config1.ini")
if config1.has_section('general'): # <== Add this
    comport = config1.get('general', 'comport')
    win_res = config1.get('general','win_res')
    hardware = config1.get('general', 'hardware')
    pins = config1.get('general', 'pins')
    state = config1.get('general', 'state')