Issue2818
Created on 2019-10-30.07:40:50 by rkanumola, last changed 2021-05-03.15:49:28 by Divakar.
Messages | |||
---|---|---|---|
msg12728 (view) | Author: RK (rkanumola) | Date: 2019-10-30.07:40:50 | |
We are using PythonInterpreter to interact of Redis script data which was invoked through Python script. And we are not able to import redis module and getting the folowing error. If the PythonInterpreter is not compatible with Redis module can you please check and give us this compatability . Traceback (most recent call last): File "<string>", line 4, in <module> ImportError: No module named redis Sample piece of code which give the above error: public static void main(String[] args) { try { Properties properties = new Properties(); properties.setProperty("python.home", "/Users/ranumola/jython2.7.1/"); properties.setProperty("python.path", "/Users/ranumola/jython2.7.1/Lib"); properties.setProperty("python.import.site", "false"); PythonInterpreter.initialize(System.getProperties(), properties, new String[] {""}); PythonInterpreter python = new PythonInterpreter(); String script = "# Python3 program to add two numbers \n" + " \n" + "num1 = 15\n" + "num2 = 12\n" + " \n" + "# Adding two nos \n" + "sum = num1 + num2 \n" + " \n" + "# printing values \n" + "print(\"Sum of {0} and {1} is {2}\" .format(num1, num2, sum)) "; String script1 = "import sys\n" + "sys.path.append(\"/Users/ranumola/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(); } } |
|||
msg12730 (view) | Author: RK (rkanumola) | Date: 2019-10-30.07:44:21 | |
Please ignore the previous sample. Take the following one for reference: 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(); } } |
|||
msg12882 (view) | Author: Jeff Allen (jeff.allen) | Date: 2019-12-24.11:09:56 | |
The import works fine for me at the console with 2.7.2b2: PS issue2818> inst\bin\jython Jython 2.7.2b2 (v2.7.2b2:b9b60766cabe, Nov 1 2019, 07:46:45) [Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_231 Type "help", "copyright", "credits" or "license" for more information. >>> import redis >>> r = redis.Redis(host='localhost', port=6379) >>> r Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>> However, the first thing I got wrong was to "pip install redis" with the wrong pip (CPython one on my path, not the one in the Jython bin directory. That reproduces the error nicely. You can tell when you use the right one because it is slow :( and you end up with a directory inst/Lib/site-packages/redis, or wherever home is for your Jython. I'll try it from Java to make sure. |
|||
msg12892 (view) | Author: Jeff Allen (jeff.allen) | Date: 2019-12-24.15:54:18 | |
Hi RK (and Happy Christmas). I had success with: // Sample piece of code for issue 2818 (RK, JA) import java.util.Properties; import org.python.util.PythonInterpreter; import org.python.core.PyString; public class RunRedis { public static void main(String[] args) { try { Properties properties = new Properties(); // You don't need these: Jython will find home (usually): //properties.setProperty("python.home", "../jython2.7.1/"); //properties.setProperty("python.path", "../jython2.7.1/Lib"); // *** This is what is stopping Jython finding site-packages/redis *** //properties.setProperty("python.import.site", "false"); PythonInterpreter.initialize(System.getProperties(), properties, new String[] {""}); PythonInterpreter python = new PythonInterpreter(); String script = "import sys\n" + // You don't need this: Jython will find home (usually): //"sys.path.append(\"../jython2.7.1/Lib\")\n" + "\n" + // See what sys.path we get without forcing it: "print sys.path\n" + "import redis\n" + "r = redis.Redis(host='localhost', port=6379)\n" + "print repr(r)\n"; //python.set("script", new PyString(script)); python.exec(script); python.close(); } catch (Exception e) { e.printStackTrace(); } } } where "success" is defined as this output: PS issue2818> javac -cp ".\inst\jython.jar" RunRedis.java PS issue2818> java -cp ".;.\inst\jython.jar" RunRedis ['...\\issue2818\\inst\\Lib', '__classpath__', '__pyclasspath__/', '...\\.local\\lib\\jython2.7\\site-packages', '...\\issue2818\\inst\\Lib\\site-packages'] Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>> If you suppress use site import, then site-packages is not on your path and redis will not be found. It's the same with "python -S" and: PS issue2818> java -D"python.import.site=false" -cp ".;.\inst\jython.jar" RunRedis ['...\\issue2818\\inst\\Lib', '__classpath__', '__pyclasspath__/'] Traceback (most recent call last): File "<string>", line 4, in <module> ImportError: No module named redis |
|||
msg13156 (view) | Author: Divakar (Divakar) | Date: 2021-05-03.15:49:28 | |
Hi RK, is the issue resolved? I'm also facing the same issue, but no luck |
History | |||
---|---|---|---|
Date | User | Action | Args |
2021-05-03 15:49:28 | Divakar | set | nosy:
+ Divakar, - jeff.allen messages: + msg13156 |
2020-02-01 08:51:14 | jeff.allen | set | status: pending -> closed |
2019-12-24 15:54:18 | jeff.allen | set | priority: normal assignee: jeff.allen status: open -> pending messages: + msg12892 resolution: invalid |
2019-12-24 11:09:56 | jeff.allen | set | nosy:
+ jeff.allen messages: + msg12882 |
2019-10-30 07:44:21 | rkanumola | set | messages: + msg12730 |
2019-10-30 07:41:03 | rkanumola | set | title: Support for Redis Module for PythonInterpreter -> Support for Redis Module in PythonInterpreter |
2019-10-30 07:40:50 | rkanumola | create |
Supported by Python Software Foundation,
Powered by Roundup