I'm trying to create tables in my database (postgresql 9.6) and when I launch my python script to do so, it returns me an error of the following type:
"Section postgresql not found in the $FILEDIR/database.ini file"
It seems like the parser cannot read the section, but I don't understand why.
This is my config method:
def config(filename='$FILEDIR/database.ini', section='postgresql'):
parser = ConfigParser()
parser.read(filename)
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
Database.ini:
[postgresql]
host=localhost
database=mydatabase
user=myuser
password=mypassword
I've tried the answers in this following thread but it does not help me at all. Anyone knows the cause? I'm using python 2.7 and I've executed "pip install config" and "pip install configparser" for dependencies.
I had the same issue aswell, it was cured by placing the whole file path into the kwarg in config:
def config(filename='/Users/gramb0t/Desktop/python-postgre/data/database.ini', section='postgresql'):
#just remove the $FILEDIR. it worked for me.
from configparser import ConfigParser
def config(filename="database.ini", section="postgresql"):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)
# get section, default to postgresql
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception(
"Section {0} not found in the {1} file".format(section, filename)
)
return db
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With