简体   繁体   English

PyhtonInterpreter所有导入均失败

[英]PyhtonInterpreter all imports are failing

Im using jython 2.7.0 and everything is working except one thing: I cant import anything. 我正在使用jython 2.7.0 ,除了一件事之外,其他所有东西都在工作:我无法导入任何东西。 I do the following: 我执行以下操作:

PythonInterpreter.initialize(props, System.getProperties(), new String[]{script.getParams().toJSON(), script.getContext().toJSON()});
PythonInterpreter interpreter = new PythonInterpreter();

interpreter.execfile(script.getPath());
interpreter.exec("import sys");
interpreter.exec("import json");
interpreter.exec("import random");

PyObject answerEvent = interpreter.eval("json.loads(sys.argv[0])");
PyObject answerContext = interpreter.eval("json.loads(sys.argv[1])");
PyObject answerResult = interpreter.eval("json.dumps(handler(sys.argv[0], sys.argv[1]))");

System.out.println("=====================================================================");
System.out.println(answerEvent.toString());
System.out.println(answerContext.toString());
System.out.println(answerResult.toString());
System.out.println("=====================================================================");

And it really does not matter which import I run, I always get the error: 而且我运行哪个导入都没关系,我总是会收到错误:

Exception in thread "main" Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named json

or 要么

Import Error: No module named random

How do I get the imports working. 如何使导入工作。

EDIT 编辑

Sorry, my information was not completely correct: import sys is working without a problem. 抱歉,我的信息不完全正确: import sys正常运行。 The other two imports arent. 其他两个进口阿伦特。

EDIT 2 编辑2

I tried a recommended workaround described here in the last post but the result was the same. 我尝试了上一篇文章中介绍的推荐解决方法但结果是相同的。

Fixed with the following: 已修复以下问题:

interpreter.exec("import sys");
interpreter.exec("sys.path.append('/usr/lib/python2.7')");
interpreter.exec("import random");
interpreter.exec("import json");

I needed to point to the lib folder of python 2.7 (But I still dont know why) 我需要指向python 2.7的lib文件夹(但我仍然不知道为什么)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM