2

i am trying to make a config file using ConfigParser in python 3.

I get this error when trying to run the script

C:\Users\Deagan>"C:\Users\Deagan\Desktop\Hypixel Rich Presence\Main.py"
Traceback (most recent call last):
  File "C:\Users\Deagan\Desktop\Hypixel Rich Presence\Main.py", line 10, in <mod
ule>
    APIKEY = config['DEFAULT']['secretid']
  File "C:\Users\Deagan\AppData\Local\Programs\Python\Python38\lib\configparser.
py", line 1254, in __getitem__
    raise KeyError(key)
KeyError: 'secretid'

Here is my config file:

[DEFAULT]
secretid = 'XXXXXXXXXX'
username = 'XXXX'
client_id = 'XXXXXXXX'

Here is the Main.py script file

config = ConfigParser()
config.read('config.ini')
APIKEY = config['DEFAULT']['secretid']
client_id = config['DEFAULT']['client_id']
username = config['DEFAULT']['username']
print(config.sections())

Am i doing something wrong?

Deagan Muir
  • 103
  • 4
  • 13

2 Answers2

1

Currently File not found error is not handled in config parser, check here https://github.com/jaraco/configparser/issues/53

Please check the config file is present in the same directory of python code.

To check file is present / not Move this Line to top,

print(config.sections())

If sections are printed then your file is ok check for typo in the section names and keys

Vignesh
  • 1,553
  • 1
  • 10
  • 25
0

Make sure the files are in the same folder and the name assigned to config.read('xxxxxx.ini') matches the one you created.