Message12892
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 |
|
Date |
User |
Action |
Args |
2019-12-24 15:54:18 | jeff.allen | set | messageid: <1577202858.83.0.21419663693.issue2818@roundup.psfhosted.org> |
2019-12-24 15:54:18 | jeff.allen | set | recipients:
+ jeff.allen, rkanumola |
2019-12-24 15:54:18 | jeff.allen | link | issue2818 messages |
2019-12-24 15:54:18 | jeff.allen | create | |
|