PascalLesage / presamples

Package to write, load, manage and verify numerical arrays, called presamples.
BSD 3-Clause "New" or "Revised" License
14 stars 11 forks source link

brightway2 silently not supporting presamples if presamples module imported first #44

Closed PascalLesage closed 5 years ago

PascalLesage commented 6 years ago

Importing presamples before brightway2 results in a brightway2 import that does not recognize presamples.


In [1]: import presamples

In [2]: from brightway2 import *

In [3]: lca = LCA({Database('example').random():1}, presamples = ['some_path'])
C:\mypy\anaconda\envs\bw\lib\site-packages\bw2calc\lca.py:90: UserWarning: Skipping presamples; `presamples` not installed
  warnings.warn("Skipping presamples; `presamples` not installed")

Importing in the opposite order does not result in this problem.

I think I traced the sequence of events leading to this problem to this, though I don't understand it: 1) When importing presamples, the PackagesDataLoader method is imported. 2) This in turn imports utils.validate_presamples_dirpath 3) The .utils in turn from bw2calc.utils import md5 , hence loading bw2calc 4) bw2calc tries to import the PackagesDataLoader. The import is in a try/except, and since presamples is not yet imported, it excepts. bw2calc is loaded without the PackagesDataLoader.

I'm unsure why the imported bw2calc is modified (and includes the PackagesDataLoader) when presamples is imported second.

A quick fix would be to replace the md5 function with something else (or simply copying it within presamples. It does not seem special enough to cause this headache.

PascalLesage commented 5 years ago

Fixed: https://github.com/PascalLesage/brightway2-presamples/pull/45/commits/1fe1ee5ba175089df51b22a75fae4674093b7e27