Python - ConfigParser - AttributeError: ConfigParser instance has no attribute '__getitem__'
Asked Answered
H

3

10

I am creating a quote of the day server. I am reading options from an INI file, whose text is below:

[Server]
host =
port = 17

[Quotes]
file=quotes.txt

However, when I use ConfigParser, it gives me this error:

Traceback (most recent call last):
  File "server.py", line 59, in <module>
    Start()
  File "server.py", line 55, in Start
    configOptions = parseConfig(filename)
  File "server.py", line 33, in parseConfig
    server = config['Server']
AttributeError: ConfigParser instance has no attribute '__getitem__'

Here is my code:

#!/usr/bin/python

from socket import *
from  ConfigParser import *
import sys

class serverConf:
    port = 17
    host = ""
    quotefile = ""

def initConfig(filename):


    config = ConfigParser()

    config['Server'] = {'port': '17', 'host': ''}
    config['Quotes'] = {'file': 'quotes.txt'}

    with open(filename, 'w') as configfile:
        config.write(configfile)


def parseConfig(filename):

    configOptions = serverConf()



    config = ConfigParser()
    config.read(filename)

    server = config['Server']

    configOptions.port = int(server['port'])
    configOptions.host = conifg['Server']['host']
    configOptions.quoteFile = config['Quotes']['file']



    print "[Info] Read configuration options"

    return configOptions

def doInitMessage():

    print "Quote Of The Day Server"
    print "-----------------------"
    print "Version 1.0 By Ian Duncan"
    print ""

def Start():

    filename = "qotdconf.ini"
    configOptions = parseConfig(filename)

    print "[Info] Will start server at: " + configOptions.host + ":" + configOptions.port

Start()

Why am I getting this error, and what can I do to fix it?

Hillyer answered 6/5, 2013 at 21:10 Comment(4)
Brackets won't work. Use the get() function. configOptions.host = conifg.get('Server','host') docs.python.org/2/library/configparser.html#examplesCello
Well, you're trying to use config as if it were a dictionary, and it's not, it's a ConfigParser instance...Qualls
In future, you might want to refer to the ConfigParser documentation.Dichogamy
this solution would work in py3Glutamine
C
15

After a quick read it seems like you're trying to read the data as if it's a dictionary, when you should use: config.get(section, data)

EG:

...
config = ConfigParser()
config.read(filename)
...
configOptions.port = config.getint('Server', 'port')
configOptions.host = config.get('Server', 'host')
configOptions.quoteFile = config.get('Quotes', 'file')

To write to the config-file you could do something like:

...
def setValue(parser, sect, index, value):
    cfgfile = open(filename, 'w')
    parser.set(sect, index, value)
    parser.write(cfgfile)
    cfgfile.close()
Country answered 6/5, 2013 at 21:19 Comment(4)
I must have read the guide on some website wrong, because I thought it said to use angle brackets.Hillyer
You could use the python docs: docs.python.org/2/library/configparser.htmlCountry
This is the difference between the python 3 version of configparser and the pyhton 2.7 version of the configparser. in python 3.3, this is what you would normally do.Ureter
this is incredibly annoying since they are named almost identically. my local python2 found configparser and used it without any problems, but it crashed my staging and this took me a while to figure out to rename it to ConfigParser and not use angle brackets. thanks for the help stackoverflow!Embryectomy
A
6

The included ConfigParser with python 2.7 does not work in this fashion. You can, however, achieve exactly what you've proposed using the back ported configparser module available on PyPy.

pip install configparser

Then you can use it just as you would in Python 3*

from configparser import ConfigParser
parser = ConfigParser()
parser.read("settings.ini")
# or parser.read_file(open("settings.ini"))
parser['Server']['port']
# '17'
parser.getint('Server', 'port')
#  17

NOTE

  • configparser is not 100% compatible with the Python 3 version.
  • The backport is intended to keep 100% compatibility with the vanilla release in Python 3.2+.
  • Using it in this fashion displayed above, will default to the Python 3 implementation if available.
Abortion answered 18/11, 2016 at 12:50 Comment(0)
A
1

for python3
iniparser.py

#!/usr/bin/python3
import configparser
import sys

config = configparser.ConfigParser()
config.read(sys.argv[1])
print(config[sys.argv[2]][sys.argv[3]])

using

python iniparser.py <filepath> <section> <key>
Aperient answered 14/4, 2021 at 3:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.