【发布时间】:2018-03-21 12:11:59
【问题描述】:
我正在尝试在我的数据库 (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
数据库.ini:
[postgresql]
host=localhost
database=mydatabase
user=myuser
password=mypassword
我已经尝试了this following thread 中的答案,但它对我一点帮助都没有。有人知道原因吗?我正在使用 python 2.7 并且我已经为依赖项执行了“pip install config”和“pip install configparser”。
【问题讨论】:
-
我不认为 python 知道
$FILEDIR是什么。尝试 .ini 文件的绝对路径。 -
天啊。就这么简单,我怎么可能没想到。非常感谢!
标签: python python-2.7 configparser