Question

config.ini

[sectionA]
username = my_username
password = my_password

[sectionB]
username = my_username

.py

import ConfigParser

config = ConfigParser.ConfigParser()
config.read("config.ini")
usernameA = config.get("sectionA", "username")

This works fine when I tested it locally, but I pushed it to Heroku and got a ConfigParser.NoSectionError when I check the Heroku logs. Both my .py file and config.ini file are in the same directory. Googling this issue shows that it might be fixable my explicitly stating the file path; how do I do this when app is on Heroku? Are there any other fixes I can try?

Was it helpful?

Solution

Try:

import ConfigParser
import os

config = ConfigParser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), r"config.ini"))
usernameA = config.get("sectionA", "username")

dirname: returns the directory of a file.

__file__: refers to the script file name

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top