一.报错代码及分析:
model_conf = yaml.load(f)
TypeError: load() missing 1 required positional argument: 'Loader'
由报错信息可见:yaml的load函数缺少"Loader"这个参数,这是因为 yaml 5.1 版本后弃用了 yaml.load(file) 这个用法。
新版的load()函数如下:
def load(stream, Loader):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
"""
loader = Loader(stream)
try:
return loader.get_single_data()
finally:
loader.dispose()
Yaml 5.1 之后的版本为了加强安全性要求指定具体的 Loader,禁止执行任意函数。
二.解决方法:
d1 = yaml.load(file, Loader=yaml.FullLoader)
d1 = yaml.safe_load(file)
d1 = yaml.load(file, Loader=yaml.CLoader)
with open(config_file) as stream:
config_dict = yaml.safe_load(stream)
以上两种方法任意一即可。