polca / premise

Coupling Integrated Assessment Models output with Life Cycle Assessment.
BSD 3-Clause "New" or "Revised" License
110 stars 48 forks source link

bw2-25 version checking (fixed origin branch) #128

Closed Stew-McD closed 11 months ago

Stew-McD commented 11 months ago

Moved from PR#125 because it was from the wrong branch.

In ecoinvent_modification.py there is checking for brightway versions via attempted import of bw_processing (which is brightway version agnostic).

In an env with bw2 and bw_processing, this will lead to premise trying to write a db with .brightway25 instead of .brightway2 which leads to errors like:

  File "/home/stew/code/gh/premise/premise/brightway25.py", line 34, in <dictcomp>
    obj.key: obj.id
AttributeError: 'Activity' object has no attribute 'id'

Ideally, there could be a check to see if the project is bw2 or bw25, but I couldn't see any easy way to do that. (I would like to know how to do that in general, so that I don't accidentally change my bw2 projects into bw25)

Maybe better just to change the import attempt to that below, no? (or did Chris have a reason to use bw2_processing?)

try:
    import bw2data

    major_version = int(bw2data.__version__.split('.')[0])
    assert major_version >= 4

    from .brightway25 import write_brightway_database
    logger.info("Using Brightway 2.5")

except AssertionError:
    from .brightway2 import write_brightway_database
    logger.info("Using Brightway 2")
cmutel commented 11 months ago

LGTM

cmutel commented 11 months ago

@romainsacchi This is a small change and seems to work, even if the CI is complaining. Can we merge it?