Python Forum
Jython macro which uses PythonInterpreter and Redis script gives an error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Jython macro which uses PythonInterpreter and Redis script gives an error (/thread-22092.html)



Jython macro which uses PythonInterpreter and Redis script gives an error - rkanumola - Oct-29-2019

Hi,

We are trying to use Jython with Redis script within confluence using a Jython macro.
We are able to execute the action using Jython from the command line and able to read the variable from redis database and get the result, but when we try to use the PythonInterpreter class from within Java application we get the below error. It would be great can suggest ,how to handle this error.

Quote:Traceback (most recent call last):
line 4, in
File “/opt/jython/Lib/site-packages/redis/ init .py”, line 1, in
from redis.client import Redis, StrictRedis
SyntaxError: (“no viable alternative at input ‘’’‘“, (‘/opt/jython/Lib/site-packages/redis/client.py’, 28, 13, ‘’))

Our execution piece of code for reference:

Quote: try {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.setOut(out);
interpreter.setErr(err);
interpreter.exec(code);
} catch (PyException exception) { // Exception from parsing script
errorText = getModifiedErrorText(exception.toString());
errorTitle = errorText.startsWith("SyntaxError") ? "Syntax error" : "Error running script";
} catch (Exception exception) { // Some other exception from the script
errorTitle = "Unexpected error running script";
errorText = exception.toString();
}

Please let us know if any thing is not clear or need more information to give a solution to this problem.


RE: Jython macro which uses PythonInterpreter and Redis script gives an error - rkanumola - Oct-29-2019

More relavant piece of code:
Quote: try {
Properties properties = new Properties();
properties.setProperty("python.home", "../jython2.7.1/");
properties.setProperty("python.path", "../jython2.7.1/Lib");
properties.setProperty("python.import.site", "false");
PythonInterpreter.initialize(System.getProperties(), properties, new String[] {""});
PythonInterpreter python = new PythonInterpreter();
String script = "import sys\n" + "sys.path.append(\"../jython2.7.1/Lib\")\n" + "\n" + "import redis\n"
+ "r = redis.Redis(host='localhost', port=6379)\n" + "r.get('foo')";
python.set("script", new PyString(script));
python.exec(script);
python.close();
} catch (Exception e) {
e.printStackTrace();
}



RE: Jython macro which uses PythonInterpreter and Redis script gives an error - rkanumola - Oct-30-2019

We are getting the following error for the above piece of sample. Even for "import redis" statemenet we are getting the error.
Quote:Traceback (most recent call last):
File "<string>", line 4, in <module>
ImportError: No module named redis