14

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.

Fatmajk
  • 1,770
  • 1
  • 13
  • 22

4 Answers4

8

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

fouroh4
  • 186
  • 1
  • 6
3
#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
dajo09
  • 31
  • 4
  • Welcome to StackOverflow. Please consider reviewing the formatting, moving out the text comment from the code to include it in the text of your answer, and add more explanation to elaborate on it. – alelom May 06 '20 at 15:27
2

the problem in path for solve this you can use os to get dir to database.ini as following example

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
params = config(filename=BASE_DIR+'\memory\database.ini', section='postgresql_pc__server')
Youssri Abo Elseod
  • 671
  • 1
  • 9
  • 23
-1

The thing is, you made a mistake. You don’t need to add or invent anything, just do this:

def config(filename=r'/database.ini', section='postgresql'):
    parser = ConfigParser()
    parser.read(filename)
    # получить раздел, по умолчанию postgresql
    db = {}

    if parser.has_section(section):
        print(section)
        params = parser.items(section)

        for param in params:
            db[param[0]] = param[1]
        else:
            raise Exception("Раздел {0} не найдено в {1} файл".format(section, filename))
    return db
    

but if you are in the main directory then (r, '/') you most likely need to remove and leave everything as in the documentation

endive1783
  • 827
  • 1
  • 8
  • 18