From: Debashish <deb...@gm...> - 2018-08-12 09:41:46
|
Hi Jeff, Adam, Thanks a ton for the prompt reply. Few clarifications: 1. The *python.home* path I gave is the *local* Python 2.7 installation path. I assume you are saying this should be *CPython* home rather. I will try this after installing CPython locally. 2. The problem is that I wish to deploy my code on Pivotal Cloud Foundry (PCF) where I would not be able to install anything else apart from the application JAR, no file-system access is available as since the code runs inside PCF containers, I cannot expect Python or CPython to be installed on that system. 3. I tried several thing to make it work (for e.g. extracted the *Lib* folder from the JAR and added it on the classpath, also renamed the JAR to *jython.jar* and added it to classpath, supplied the Lib folder path as *python.path* as well), none of these worked. It seems if there is no way to supply the absolute path, then there is no way to make it work from within a Java JAR. 4. My Python script is current 400 LOCs and will keep on growing, so I cannot possibly call its *functions* from my Java code as it would couple it to the Python code. I guess the only better option would be to port this Python code to Java. 5. I am using Jython Standalone JAR 2.7.1 from Maven central. I think what you are saying is that instead of passing command line arguments (argv) to the static *initialize* method I can pass it to the *PythonInterpreter* object rather. I am not sure how, as I do not such any such method in the API. Is there a code example available? Thanks again for your help. Deb On Sun, 12 Aug 2018 at 03:26, Adam Burke <ada...@gm...> wrote: > Hi Deb > > On top of that, depending on how the script you’re calling is written, it > might be useful to call one layer down instead of using I/O. Jython lets > you work with python objects fairly directly from Java, and call Python > functions directly as well. So if your python script has internal functions > and classes, you could call straight into them. > > YMMV. > > Adam > > > 在 2018年8月12日,上午7:41,Jeff Allen <ja...@fa...> 写道: > > > > I don't know anything about Spring Boot, but the standalone JAR must be > on your path for you to be able to refer to "PythonInterpreter" in your > code. So by "not detected" I guess you mean that Jython appear not to find > its library (which is in the standalone JAR). Jython guesses based on the > path to the JAR it seems to be running from (that org.python.core.Py was > loaded from) but telling it is safer. > > > > This bit struck me as odd: > > > > props.put("python.home", "C:\\Dev\\Python27"); > > > > That's not the location of CPython is it? That would cause you a world > of confusion. > > > > However, none of that addresses your question about argv (meaning > sys.argv I suppose). The important observation here is that > "PythonInterpreter.initialize" is a static method that sets a default argv > that all interpreters will see as they are created. It makes a big > difference now what version you are using. In 2.7.0, all interpreters were > really the same interpreter: you got a separate namespace for your main, > but the modules where all the same, in particular every interpreter shared > sys. In 2.7.1, each interpreter gets its own sys, and so each module used > is loaded again for each interpreter. > > > > I cannot say off the top of my head what the behaviour of the default > argv and sys.argv is, but I'm pretty sure the interpreter you make in the > next line has its own sys and the sys.argv you could set independently, > although not in the constructor. > > > > Jeff Allen > > > >> On 11/08/2018 20:16, Debashish wrote: > >> Hi, > >> > >> I am trying to call a Python Script from a Spring Boot applications. > There are two issues I am facing: > >> (1) The Jython Standalone JAR is not detected and I am forced to supply > the local install path as "python.home" to make it work. > >> (2) Once I initialize the /PythonInterpreter/ class, it seems it caches > the argv I supply to it as for the subsequent invocation it uses the same > argv values. I do call the close() method to do cleanup, but it doesn't > help :( > >> > >> Properties preprops = System.getProperties(); > >> Properties props = new Properties(); > >> > >> props.put("python.home", "C:\\Dev\\Python27"); > >> props.put("python.console.encoding", "UTF-8"); > >> props.put("python.security.respectJavaAccessibility", "false"); > >> props.put("python.import.site", "false"); > >> > >> PythonInterpreter.initialize(preprops, props, arguments); > >> PythonInterpreter pyInterpreter = new PythonInterpreter(); > >> > >> try { > >> resource = new ClassPathResource("mypyscript.py"); > >> out = new ByteArrayOutputStream(); > >> err = new ByteArrayOutputStream(); > >> pyInterpreter.setOut(out); > >> pyInterpreter.setErr(err); > >> pyInterpreter.execfile(resource.getInputStream()); > >> result[0] = out.toString(); // reading the output > >> result[1] = err.toString(); // reading any error > >> } catch (Exception e) { > >> throw new Exception(e); > >> } finally { > >> try { > >> if (out != null) > >> out.close(); > >> if (err != null) > >> err.close(); > >> pyInterpreter.close(); > >> } catch (IOException e) { > >> e.printStackTrace(); > >> } > >> } > >> > >> Please help. > >> > >> Thanks, > >> > >> Deb > >> > >> > >> > ------------------------------------------------------------------------------ > >> Check out the vibrant tech community on one of the world's most > >> engaging tech sites, Slashdot.org! http://sdm.link/slashdot > >> > >> > >> _______________________________________________ > >> Jython-users mailing list > >> Jyt...@li... > >> https://lists.sourceforge.net/lists/listinfo/jython-users > > > > > > > ------------------------------------------------------------------------------ > > Check out the vibrant tech community on one of the world's most > > engaging tech sites, Slashdot.org! http://sdm.link/slashdot > > _______________________________________________ > > Jython-users mailing list > > Jyt...@li... > > https://lists.sourceforge.net/lists/listinfo/jython-users > > > ------------------------------------------------------------------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, Slashdot.org! http://sdm.link/slashdot > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > |