How to fix Python’s ConfigParser or RawConfigParser’s raise KeyError(key) configparser.py”,__getitem__
I grabbed fewPython codes to get some work done and encountered a following error while running Python. I will hide few items that don’t need.
Basically, this file was looking for config.ini file and supposed to load into a config variable. An odd thing was that I expected this code to create a file config.ini if it cannot be found, at least that was what is described in its documentation.
By doing some research, I narrowed down that this error can happen under one of following conditions:
- Possibility #1: config.ini cannot be found
- Possibility #2: Required value in config.ini cannot be found
To tackle first possibility, I manually created a file called config.ini and ran the script again. However, a same error occurred. I thought it was maybe that the file could not be found because it was looking in a different file path. So, I added following lines in that Python code to debug.
import os // ADD this to early in the code
path = os.path.join(os.getcwd(), ‘config.ini’) // Add this somewhere where you want to debug
print(‘path: ‘ + path) // Add this right after path declaration above