This article describes the method of calling Python scripts in Java programs. Share it for your reference, as follows:
In program development, sometimes you need to call related Python scripts in Java programs. The following content records the steps to get started and solutions to possible problems.
1. Create a new Maven project in Eclipse;
2. After adding the following dependency package to the pom.xml file, update maven project;
<dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.0</version></dependency><dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version></dependency>
3. Write the following test code;
import org.python.util.PythonInterpreter;public class JpythonScript { public static void main(String args[]) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); }}4. Test:
The following error occurs:
console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
* sys.path: ['...python//jython//2.7.0//Lib', '__classpath__', '__pyclasspath__/']
This attribute might be including the wrong directories, such as from CPython
* sys.prefix:***/jython/2.7.0
This attribute is set by the system property python.home, although it can
be often automatically determined by the location of the Jython jar file
You can use the -S option or python.import.site=false to not import the site module
5. Problem solving:
The refactoring code is as follows:
import java.util.Properties;import org.python.util.PythonInterpreter;public class JpythonScript { public static void main(String args[]) { Properties props = new Properties(); props.put("python.home", "path to the Lib folder"); props.put("python.console.encoding", "UTF-8"); props.put("python.security.respectJavaAccessibility", "false"); props.put("python.import.site", "false"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); }}6. Compilation was successful.
7. Reference for solving problems:
http://bugs.jython.org/issue2355
Supplement: Solution to jpython error throwing Cannot import site module
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
public class JpythonScript { public static void main(String args[]) { Properties props = new Properties(); props.put("python.import.site", "false"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); } // Perform complex class acceptance processing Map<String, Object> res = new HashMap<String, Object>();res.put("1", "Danny");res.put("2", "Fanny");PythonInterpreter interpM = new PythonInterpreter();interpM.execfile("./src/com/DataDeal.py");PyFunction pyFunctionM = (PyFunction) interpM.get("main", PyFunction.class);Map<PyObject, PyObject> tableM = new HashMap<PyObject, PyObject>();tableM.put(new PyString("conf"), PyJavaType.wrapJavaObject(res));PyDictionary pydM = new PyDictionary(tableM);For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.