关键:使用 configparser 模块来读取ini文件导入参数
首先,创建一个config.ini文件:
[debug]
log_errors=true
show_warnings=False
[server]
port: 8080
nworkers: 32
pid-file=/tmp/spam.pid
root=/www/root
然后,在test模块去读取配置:
from configparser import ConfigParser
cfg = ConfigParser()
cfg.read('config.ini')
print(cfg.sections())
print(cfg.get('debug', 'log_errors'))
print(cfg.getint('server', 'port'))
#result:
['debug', 'server']
true
8080