简体   繁体   中英

ConfigParser raise KeyError(key) Python 3.8

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM