openlca-python-examples icon indicating copy to clipboard operation
openlca-python-examples copied to clipboard

Running files to change openLCA values from a jython interpreter.

Open wardevJ opened this issue 5 years ago • 4 comments

I have been attempting to use the update parameter example listed in the code section of this tutorial to make changes to openLCA from a separate jython interpreter. I have attempted to use the code mentioned in answers to the question of how to run openLCA from outside without Eclipse, although this doesn't seem to be working with my version of jython (2.7.2) or openLCA (1.7.4), as it cannot seem to find the openlca module from the import lines:

from org.openlca.core.database.derby import DerbyDatabase
from org.openlca.core.database import ParameterDao

ImportError: No module named openlca

Are there any steps I am missing? Have I got the wrong location for the lib file?

Full code:

import os
import sys

OLCA_LIB_DIR = "C:/Users/wardevj/openlca-data-1.4/html/olca-app/libs"

for jar in os.listdir(OLCA_LIB_DIR):
  print("Add jar to path " + jar)
  full_path = os.path.join(OLCA_LIB_DIR, jar)
  sys.path.append(full_path)

from java.io import File
from org.openlca.core.database.derby import DerbyDatabase
from org.openlca.core.database import ParameterDao

if __name__ == '__main__':
  db_dir = File('C:/Users/wardevj/openLCA-data-1.4/databases/ecoinvent_35_lcia_method_20181210')
  db = DerbyDatabase(db_dir)
  dao = ParameterDao(db)
  param = dao.getForName('test_value')[1]
  param.value = 42.0
  dao.update(param)
  db.close()

Run in jython 2.7.2 interpreter with command execfile("filename.py")

wardevJ avatar Apr 30 '20 14:04 wardevJ

Have I got the wrong location for the lib file?

Probably yes, because it is unlikely that you copied the openLCA libraries into the html-folder of the openLCA workspace openlca-data-1.4/html/olca-app/libs

You need to select the folder of the openLCA installation where the script can find the following jar files:

image

msrocka avatar Apr 30 '20 16:04 msrocka

This has solved my original issue but I have run into an issue with the DerbyDatabase import, where it says "No appenders could be found for logger (org.openlca.core.database.derby.DerbyDatabase)." This is almost identical to the Cannot open Database issue logged earlier by givoigt, so I'll update my openLCA version to see if that works.

wardevJ avatar Apr 30 '20 21:04 wardevJ

No appenders could be found for logger (org.openlca.core.database.derby.DerbyDatabase).

This is not critical, it just says that it cannot find the logger configuration. This should not stop the script from running.

msrocka avatar May 01 '20 08:05 msrocka

The update to 1.10.2 worked. As long as openLCA doesn't have the database open, it updates the parameter as expected. Thanks for your help msrocka.

wardevJ avatar May 01 '20 11:05 wardevJ