import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import org.python.core.Py; import org.python.core.PyObject; import org.python.core.PyString; import org.python.core.PySystemState; import org.python.util.PythonInterpreter; public class JythonEncodingIssue { static final String a = "a"; static final String ae = "ä"; public static void main(String[] args) throws Exception { // Write file.py into disk. String pythonProgram = "# -*- coding: utf-8 -*-\n" + "a = '" + a + "'\n" + "ae = '" + ae + "'\n"; OutputStreamWriter outStream = new OutputStreamWriter(new FileOutputStream(new File("file.py")), "UTF-8"); outStream.write(pythonProgram); outStream.close(); // Put current working directory in sys.path to import from there. PySystemState sys = Py.getSystemState(); sys.path.append(new PyString(System.getProperty("user.dir"))); // Import the file created above and get a couple of variables from there. PythonInterpreter interp = new PythonInterpreter(); interp.exec("import file"); interp.exec("a = file.a"); interp.exec("ae = file.ae"); PyObject pyA = interp.get("a"); PyObject pyAe = interp.get("ae"); System.out.println("" + pyA); // a System.out.println("" + pyAe); // ä System.out.println("" + a.equals(pyA.toString())); // true System.out.println("" + ae.equals(pyAe.toString())); // false ! } }