Forums

NoSectionError raised by ConfigParser when tried to load a configuration file

Hello everyone.

I have a working project on my local machine. I am getting a NoSectionError raised by the ConfigParser module. The weird thing is that the project works fine on local and I am 100% sure that the section is there (I mean the file I am trying to read is called apikey.cfg and literally has one section [Keys] and then i have two apikeys I am using).

I have found a similar thread , and it seemed there that the problem was coming from relative/absolute paths. So I tried making my relative paths into absolute by importing os and using os.path.abspath('apikey.cfg'). But again, this also works on local but raises the same error from PythonAnywhere.

Any idea what is the problem here? Thanks in advance for the interest.

get the absolute path from __file__. otherwise you are getting the absolute path of the wrong location. try printing out the path to see what it is. You can also tweak the working directory settings in your webapp.

Well thanks a lot for the quick reply :). Unfortunately I am not sure I understand your solution, what does

get the absolute path from file

mean?

This is clearly just a Python question now , so I don't know if it is the right place to ask, but what would be a sample code?

Would I still need to use the os module? Just for you to know this one piece of the code that is causing the trouble:

class tescoSearch():
# constructor for the search object
def __init__(self, query,offset = 0, limit = 10):
config = ConfigParser.RawConfigParser()
abspath = os.path.abspath('apikey.cfg')
config.read(abspath)

self.API_KEY = config.get('Keys','API_TESCO')        
    self.query = query
    self.offset = offset
    self.limit = limit
    self.data = self.getData()

How would I change that to use the file thing? Meanwhile i'll use those suggestion you gave me to try to figure out an answer. Thanks for the support!

So for example, os.path.abspath(os.path.dirname(__file__)) will give you the folder that is containing your file.