Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Section postgresql not found in the database.ini file

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.

like image 361
Fatmajk Avatar asked Mar 21 '18 12:03

Fatmajk


2 Answers

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'):

like image 50
fouroh4 Avatar answered Sep 18 '22 17:09

fouroh4


#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
like image 33
dajo09 Avatar answered Sep 20 '22 17:09

dajo09