Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
__getitem__ Error
#1
Hi,

I have this code below:

import configparser
import graph

def main():
    # print('Python Graph Tutorial\n')

    # Load settings
    config = configparser.ConfigParser()
    config.read(['config.cfg', 'config.dev.cfg'])
    azureSettings = config['azure']

main()
And I'm getting this error:

Error:
Traceback (most recent call last): File "C:\Hosea\pythonProjects_Test\main.py", line 16, in <module> main() File "C:\Hosea\pythonProjects_Test\main.py", line 14, in main azureSettings = config['azure'] File "C:\Users\S951378\AppData\Local\Programs\Python\Python39\lib\configparser.py", line 960, in __getitem__ raise KeyError(key) KeyError: 'azure'
Please help.
Reply
#2
Please, check that files 'config.cfg' and 'config.dev.cfg' are in current working directory and show/check their content
you can check which files were parsed successfully by cheeking the return value from calling read()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jun-15-2022, 07:37 AM)buran Wrote: Please, check that files 'config.cfg' and 'config.dev.cfg' are in current working directory and show/check their content
you can check which files were parsed successfully by cheeking the return value from calling read()

Hi,
where do I get those files, my code is hitting configparser, which is in the Lib.
Reply
#4
You are trying to read 2 config files, you are the one that wrote this line
config.read(['config.cfg', 'config.dev.cfg'])
How shall we know why do you read these files or where they are located. Your current code suggest they are in current working directory.

Isn't main.py a file you write?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jun-15-2022, 08:21 AM)buran Wrote: You are trying to read 2 config files, you are the one that wrote this line
config.read(['config.cfg', 'config.dev.cfg'])
How shall we know why do you read these files or where they are located. Your current code suggest they are in current working directory.

Isn't main.py a file you write?

oh sorry my bad, I had to the file but didn't copy it to my working directory, it in my old code, got confused there a bit. It is now working thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __getitem__ is readonly problem harun2525 4 5,867 May-10-2017, 05:44 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020