Jython :: PythonInterpreter可以使用哪些模块以及如何添加更多模块(Jython :: which modules are available to PythonInterpr

编程入门 行业动态 更新时间:2024-10-28 06:22:18
Jython :: PythonInterpreter可以使用哪些模块以及如何添加更多模块(Jython :: which modules are available to PythonInterpreter and how to add more)

我使用Jython 2.5.3 PythonInterpreter类来评估一些简单的脚本,但是当我需要导入任何非核心模块时,我得到一个例外。 我是否必须在CLASSPATH中添加一些jython库jar?

缩小代码来演示问题:

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); } }

在CLASSPATH中仅使用jython-2.5.3.jar运行上述操作会失败,并显示以下跟踪:

[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)

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)

更多推荐

本文发布于:2023-08-07 05:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   可以使用   更多   PythonInterpreter   Jython

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!