From: Menelaos P. <mpe...@gm...> - 2013-02-08 13:51:12
|
I am using the *Jython 2.5.3* *PythonInterpreter* class to evaluate some simple scripts but when I need to import any non-core modules I get an exception. Do I have to add some jython library jars in the CLASSPATH? Narrow-down code that demonstrates the problem: import org.python.core.*;import org.python.util.PythonInterpreter; public class JythonTest { public static void main(String args[]) throws Exception { String scriptA = "import json"; // "import datetime" fails as well PythonInterpreter pi = new PythonInterpreter(); PyCode code = pi.compile(scriptA); PyObject result = pi.eval(code); }} Running the above with only *jython-2.5.3.jar* in the CLASSPATH fails with the following trace: [java] ImportError: No module named json [java] [java] at org.python.core.Py.ImportError(Py.java:304) [java] at org.python.core.imp.import_first(imp.java:755) [java] at org.python.core.imp.import_module_level(imp.java:837) [java] at org.python.core.imp.importName(imp.java:917) [java] at org.python.core.ImportFunction.__call__(__builtin__.java:1220) [java] at org.python.core.PyObject.__call__(PyObject.java:357) [java] at org.python.core.__builtin__.__import__(__builtin__.java:1173) [java] at org.python.core.imp.importOne(imp.java:936) [java] at org.python.pycode._pyx0.f$0(<script>:2) [java] at org.python.pycode._pyx0.call_function(<script>) [java] at org.python.core.PyTableCode.call(PyTableCode.java:165) [java] at org.python.core.PyCode.call(PyCode.java:18) [java] at org.python.core.Py.runCode(Py.java:1275) [java] at org.python.core.__builtin__.eval(__builtin__.java:484) [java] at org.python.core.__builtin__.eval(__builtin__.java:488) [java] at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:198) [java] at JythonTest.main(JythonTest.java:10) |
From: Jeff A. <ja...@fa...> - 2013-02-09 08:21:52
|
Hi Menelaos. The methods at the top of your stack trace implement the import statement. It is not CLASSPATH that is your problem, I would say, but the path that controls the search for Python modules. This is helpful: http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html#module-search-path-and-loading I'm sometimes bitten by this in my IDE when I forget to add a JVM argument -Dpython.home= ... , or rather when Eclipse makes up a configuration without it. In that case, the standard library is missing from the search, and sys.path is just: ['__classpath__', '__pyclasspath__/'] When that happens, you can only find the built-in modules (such as sys, os, etc.), and Java runtime. (When running Jython at the command prompt, a wrapper script is invoked that defines an awful lot of things that will be missing if you just launch the JAR.) If you can't find 'datetime', that may be your problem. If your import succeeds for standard library modules, but not for extra modules you want (I think json is not standard in 2.5.3), then you probably need to define or add the location to python.path, either in the (Jython) registry or as a JVM argument. Again, it should show up in the ultimate value of sys.path. This discusses how to do it: http://www.jython.org/archive/22/userfaq.html#my-modules-can-not-be-found-when-imported-from-an-embedded-application The search strategy is quite complicated because it has to be like Python and like Java at the same time. Jeff Allen On 08/02/2013 13:51, Menelaos Perdikeas wrote: > > I am using the *Jython 2.5.3* /PythonInterpreter/ class to evaluate > some simple scripts but when I need to import any non-core modules I > get an exception. Do I have to add some jython library jars in the > CLASSPATH? > > Narrow-down code that demonstrates the problem: > > |import org.python.core.*; > import org.python.util.PythonInterpreter; > > public class JythonTest { > > public static void main(String args[]) throws Exception { > String scriptA= "import json"; // "import datetime" fails as well > PythonInterpreter pi= new PythonInterpreter(); > PyCode code= pi.compile(scriptA); > PyObject result= pi.eval(code); > } > }| > > Running the above with only *jython-2.5.3.jar* in the CLASSPATH fails > with the following trace: > > | [java] ImportError: No module named json > [java] > [java] at org.python.core.Py.ImportError(Py.java:304) > [java] at org.python.core.imp.import_first(imp.java:755) > [java] at org.python.core.imp.import_module_level(imp.java:837) > [java] at org.python.core.imp.importName(imp.java:917) > [java] at org.python.core.ImportFunction.__call__(__builtin__.java:1220) > ...| |
From: Alan K. <jyt...@xh...> - 2013-02-09 11:11:08
|
[Menelaos] > I am using the Jython 2.5.3 PythonInterpreter > String scriptA = "import json"; // "import datetime" fails as well > [java] ImportError: No module named json An important point to note is that there is no json module in jython 2.5, because it was not introduced into cpython until 2.6. So jython 2.7 has a json module. If you cannot move to 2.7, I published a jython specific json module, called Jyson, which has a very similar interface to the json module. http://jyson.xhaus.com/ Which will be much faster then the json module, on both 2.5 and 2.7, since it is written in native java. Alan. |