ConfigParser cannot pop section that exists with a no section error

44 Views Asked by At

I'm trying to make a settings manager for my UI app and I keep getting this error in the below code:

class SettingsManager:
    def __init__(self, settings_file: str):
        if not os.path.isfile(settings_file):
            open(settings_file, "w").close()

        self._settings_file = settings_file
        self._config = ConfigParser()
        self._config.read(settings_file)

        # Backwards compatibility
        if self._config.has_section("user_settings"):
            # Rename "user_settings" section to "main" section.
            self._config["main"] = self._config.pop("user_settings")
  File "/Users/test/programming/onGAU/onGAU/settings_manager.py", line 19, in __init__
    self._config["main"] = self._config.pop("user_settings")
    ~~~~~~~~~~~~^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/configparser.py", line 993, in __setitem__
    self.read_dict({key: value})
  File "/opt/homebrew/Cellar/[email protected]/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/configparser.py", line 763, in read_dict
    for key, value in keys.items():
  File "<frozen _collections_abc>", line 860, in __iter__
  File "/opt/homebrew/Cellar/[email protected]/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/configparser.py", line 1292, in __iter__
    return self._options().__iter__()
           ^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/configparser.py", line 1296, in _options
    return self._parser.options(self._name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/configparser.py", line 690, in options
    raise NoSectionError(section) from None
configparser.NoSectionError: No section: 'user_settings'

And here's my .ini file:

[user_settings]
model = a
prompt = a
negative_prompt = a
seed = 123
pipeline = Text2Img
scheduler = DPMSolverMultistepScheduler Karras
base_image_path =
guidance_scale = 9.0
step_count = 2
image_amount = 2
width = 720
height = 480
safety_checker = True
attention_slicing = True
vae_slicing = False
xformers_memory_attention = False
compel_weighting = False
clip_skip = 2
lpwsd_pipeline = True
upscale_amount = 2

I've also tried:

self._config.add_section("main")
self._config["main"] = self._config.pop("user_settings")

and

section = self._config.pop("user_settings")
self._config["main"] = section

but both also don't work.

I can print the section's values before and I even check if the section exists first before popping it. Why does this still happen? Thanks in advance!

0

There are 0 best solutions below