java - Load Python class through Jython -
i'm trying load python class embedding jython in java application.
the code have far is
string pythonroot = main.class.getresource("/python").getpath(); pysystemstate state = new pysystemstate(); pyobject importer = state.getbuiltins().__getitem__(py.newstring("__import__")); pyobject sysmodule = importer.__call__(py.newstring("sys")); final pystring pythonpath = py.newstring(pythonroot); pylist path = (pylist) sysmodule.__getattr__("path"); path.add(pythonpath); pymodule module = (pymodule) importer.__call__(py.newstring("building.blah.again.blah2.test")); pyobject klass = module.__getattr__(py.newstring("address")); addressinterface ai = (addressinterface) klass.__call__().__tojava__(addressinterface.class);
the class i'm trying access can found in
/python/building/blah/again/blah2/test.py
and name of class is
address
however, gives me exception
exception in thread "main" importerror: no module named blah2
if place file in directory above, so
/python/building/blah/again/test.py
this gives me exception
exception in thread "main" importerror: no module named again
it's if actively refusing recognize directories contains files. can problem here , how might proceed around this?
if added path of module python-path, did via path.add(pythonpath);
, import-command expects name of module, not full path, i.e. pymodule module = (pymodule) importer.__call__(py.newstring("test"));
further, should confirm right path added printing contents of path-list. note class-declaration in test.py must extend address-java-interface tojava
work (i mention because common source of error).
that said, way of using jython appears cumbersome me. if you, stuff (adding path, doing import) in python-script, run via org.python.util.pythoninterpreter
(http://www.jython.org/javadoc/org/python/util/pythoninterpreter.html) , retrieve class-pyobject via eval
or get
-method.
Comments
Post a Comment