在database.ini文件中找不到节postgresql

Fat*_*ajk 6 python configparser python-2.7

我正在尝试在数据库(PostgreSQL 9.6)中创建表,并且当我启动python脚本这样做时,它返回以下类型的错误:

“在$ FILEDIR / database.ini文件中找不到节postgresql”

似乎解析器无法阅读本节,但我不明白为什么。

这是我的配置方法:

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
Run Code Online (Sandbox Code Playgroud)

Database.ini:

[postgresql]
host=localhost
database=mydatabase
user=myuser
password=mypassword
Run Code Online (Sandbox Code Playgroud)

我已经尝试了以下线程中的答案,但它完全没有帮助。有人知道原因吗?我使用的是python 2.7,并且已经执行了“ pip install config”和“ pip install configparser”的依赖项。

小智 8

我也遇到了同样的问题,通过将整个文件路径放入配置中的 kwarg 中来解决:

def config(filename='/Users/gramb0t/Desktop/python-postgre/data/database.ini', section='postgresql'):