Import error when using the python API

Post Reply
mati
Posts: 6
Joined: 13 Sep 2017 11:38

Import error when using the python API

Post by mati » 13 Sep 2017 12:29

Hi,

I'm testing out the openLCA python API and I really hope I can soon run calculations in bulk through it.
I'm starting slowly, using the openLCA python tutorial on GitHub (many thanks for that). I'm trying to use the functions defined in util.py in the GitHub repo.
I copied the file util.py on my computer under /Users/mathieusa/openLCA-data-1.4/python and I'm running a script in the python-developer-tool-console in openLCA 1.4.2 on Mac OS X Yosemite (10.10.5).
I get an import error. Please see the log below.
Note: the same import error happens if I run "from org.openlca.core.database import Daos" directly in the python console in openLCA.

Can someone point me to what I'm doing wrong?

Best wishes,
Mathieu

python script:

Code: Select all

from org.openlca.core.database.derby import DerbyDatabase as Db
from java.io import File
import org.openlca.core.model as model
from org.openlca.core.database import ProcessDao
import util

if __name__ == '__main__':
    db_dir = File('/Users/mathieusa/openLCA-data-1.4/databases/phd_ei22_ist_test')
    db = Db(db_dir)
    
    flow = util.find(db, model.Flow, 'Electricity, wind, onshore, MIX IST')
 


log file:

Code: Select all

49199005 	Worker-28 	ERROR 	org.openlca.app.devtools.python.Python 	failed to evaluate script
Traceback (most recent call last):
     File "<string>", line 44, in <module>
     File "/Users/mathieusa/openLCA-data-1.4/python/util.py", line 1, in <module>
     from org.openlca.core.database import Daos
    ImportError: cannot import name Daos
    
     at org.python.core.Py.ImportError(Py.java:300)
     at org.python.core.imp.importFromAs(imp.java:1052)
     at org.python.core.imp.importFrom(imp.java:1016)
     at util$py.f$0(/Users/mathieusa/openLCA-data-1.4/python/util.py:52)
     at util$py.call_function(/Users/mathieusa/openLCA-data-1.4/python/util.py)
     at org.python.core.PyTableCode.call(PyTableCode.java:166)
     at org.python.core.PyCode.call(PyCode.java:18)
     at org.python.core.imp.createFromCode(imp.java:393)
     at org.python.core.imp.createFromSource(imp.java:356)
     at org.python.core.imp.loadFromSource(imp.java:583)
     at org.python.core.imp.find_module(imp.java:480)
     at org.python.core.imp.import_next(imp.java:737)
     at org.python.core.imp.import_module_level(imp.java:856)
     at org.python.core.imp.importName(imp.java:946)
     at org.python.core.ImportFunction.__call__(__builtin__.java:1301)
     at org.python.core.PyObject.__call__(PyObject.java:373)
     at org.python.core.__builtin__.__import__(__builtin__.java:1254)
     at org.python.core.imp.importOne(imp.java:965)
     at org.python.pycode._pyx15.f$0(<string>:51)
     at org.python.pycode._pyx15.call_function(<string>)
     at org.python.core.PyTableCode.call(PyTableCode.java:166)
     at org.python.core.PyCode.call(PyCode.java:18)
     at org.python.core.Py.runCode(Py.java:1312)
     at org.python.core.Py.exec(Py.java:1356)
     at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:222)
     at org.openlca.app.devtools.python.Python.doEval(Unknown Source)
     at org.openlca.app.devtools.python.Python.eval(Unknown Source)
     at org.openlca.app.devtools.python.PythonEditor.lambda$0(Unknown Source)
     at org.openlca.app.devtools.python.PythonEditor$$Lambda$36/961994506.run(Unknown Source)
     at org.openlca.app.WrappedJob.run(Unknown Source)
     at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54) 

mati
Posts: 6
Joined: 13 Sep 2017 11:38

Re: Import error when using the python API

Post by mati » 15 Sep 2017 12:35

Update on my original post:
I tried the same python script on a fresh install of openLCA's latest version (1.6.3) on a Linux machine (ubuntu 16.04) and I did not get the import error.

mati
Posts: 6
Joined: 13 Sep 2017 11:38

Re: Import error when using the python API

Post by mati » 18 Sep 2017 13:00

Another update on my post:
I tried the same python script on a fresh install of openLCA's latest version (1.6.3) on my Mac OS machine (10.10.5) and I did not get the import error.
I guess this closes this thread: when using open LCA 1.6.3 the command "from org.openlca.core.database import Daos" does not return an import error. That's good because this import is necessary to use the utility functions defined in "util.py" on the GitHub openlca-python-tutorial.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest