-1

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.

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • 1
    you need to paste your code and error log so other people could help you – linpingta Jun 16 '23 at 01:43
  • A thing I've noticed about configparser is that, if the file doesn't exist, it loads an empty config rather than complaining about it. Another thing is that files are located relative to your current directory, not the script's directory. – Ouroborus Jun 16 '23 at 02:03
  • @Ouroborus hmm is there maybe a way to point it towards that specific directory? So I wouldn't have gotten an error when I declared config1.read("config1.ini") ? – ryan jones Jun 16 '23 at 02:24
  • I guess I could change the directory of where the bash is calling the python script from? or put it in the same as the python script – ryan jones Jun 16 '23 at 02:25
  • Yep, that worked! Made a bash file that changed the directory to the appropriate folder, then the bash called the python script. Made a desktop shortcut that called the bash file. Worked! Thank you for the insight. I was at a dead end. – ryan jones Jun 16 '23 at 02:50

1 Answers1

0

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')
toyota Supra
  • 3,181
  • 4
  • 15
  • 19