You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Brightway25多功能单元LCIA分析中AttributeError问题求助

Brightway25中调用get_multilca_data_objs时出现AttributeError: 'MethodConfig' object has no attribute 'get'

问题原因

你遇到的错误是因为bw2data.get_multilca_data_objs函数的method_config参数预期接收字典类型,但你传入的是bw2calc.MethodConfig实例——这是一个Pydantic模型类,没有字典的get()方法,导致代码在尝试调用method_config.get("impact_categories", [])时失败。

从你使用的dev版本(bw2calc 2.0.dev23、bw2data 4.0.dev59)来看,当前代码实现和官方教程的示例可能存在版本不匹配的情况。

解决方案

你可以通过两种方式修正:

方式1:直接传入字典代替MethodConfig

不需要创建MethodConfig实例,直接构造符合要求的字典传入:

import bw2data as bd
import bw2calc as bc
import bw2io as bi
import numpy as np

bd.projects.set_current(name='carlos-bw25')
ei = bd.Database('ecoinvent-3.10-cutoff')
m = bd.methods

# activities
pp_search = [a for a in ei if 'polypropylene' in a['name'] and 'granulate' in a['name'] and a['location'] == 'GLO' ]
hdpe_search = [a for a in ei if 'polyethylene' in a['name'] and 'high density' in a['name'] and a['location'] == 'GLO' ]
ldpe_search = [a for a in ei if 'polyethylene' in a['name'] and 'low density' in a['name'] and not 'linear' in a['name'] and not 'packaging' in a['name'] and a['location'] == 'GLO' ]
activities = [pp_search[0], hdpe_search[0], ldpe_search[0]]
pp, hdpe, ldpe = activities  # 补充变量赋值,避免NameError

# LCIA method
ef_list = [m for m in bd.methods if 'EF' in str(m) and 'v3.1' in str(m) and 'LT' not in str(m) and 'EN15804' not in str(m)]
lcia_method = [ef_list[0], ef_list[1], ef_list[8], ef_list[-5]]

# LCIA
functional_units = {
    "pp": {pp.id: 1},
    "hdpe": {hdpe.id: 1},
    "ldpe": {ldpe.id: 1},
}

# 直接使用字典作为method_config参数
method_config_dict = {"impact_categories": lcia_method}
data_objs = bd.get_multilca_data_objs(functional_units, method_config_dict)

方式2:将MethodConfig实例转换为字典

如果坚持使用MethodConfig,可以通过Pydantic模型的dict()方法将其转为字典:

methodconfig = bc.MethodConfig(impact_categories=lcia_method)
data_objs = bd.get_multilca_data_objs(functional_units, methodconfig.dict())

额外说明

原代码中还有一处小问题:functional_units里用到了pp.id、hdpe.id、ldpe.id,但你只定义了activities列表却没有将元素赋值给对应变量,运行时会报NameError,上面的修正代码已经补充了这部分赋值。

内容的提问来源于stack exchange,提问作者Carlos Hernandez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 12:07:37