Java Code Examples for org.python.util.PythonInterpreter#initialize()
The following examples show how to use
org.python.util.PythonInterpreter#initialize() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JythonTest.java From phoebus with Eclipse Public License 1.0 | 6 votes |
private void init() { // System.out.println("Initializing on " + Thread.currentThread().getName()); final Properties pre_props = System.getProperties(); final Properties props = new Properties(); String home = PythonInterpreter.class .getProtectionDomain().getCodeSource().getLocation().toString(); System.out.println("Jython home: " + home); assertThat(home, not(nullValue())); if (home.contains(".jar")) System.out.println("Jython provided as JAR"); home = home.replace("file:", ""); props.setProperty("python.home", home); // props.setProperty("python.executable", "None"); props.setProperty(RegistryKey.PYTHON_CACHEDIR_SKIP, "true"); props.setProperty("python.import.site", "false"); // props.setProperty("python.console.encoding", "UTF-8"); props.setProperty("python.path", "/tmp/always"); // props.setProperty("python.verbose", "debug"); // Options.verbose = Py.DEBUG; PythonInterpreter.initialize(pre_props, props, new String[0]); }
Example 2
Source File: ScriptRunner.java From AndroidRobot with Apache License 2.0 | 6 votes |
private void initPython(String executablePath, Collection<String> pythonPath, String[] argv) /* */ { /* 160 */ Properties props = new Properties(); /* */ /* 163 */ StringBuilder sb = new StringBuilder(); /* 164 */ sb.append(System.getProperty("java.class.path")); /* 165 */ for (String p : pythonPath) { /* 166 */ sb.append(":").append(p); /* */ } /* 168 */ props.setProperty("python.path", sb.toString()); /* */ /* 173 */ props.setProperty("python.verbose", "error"); /* */ /* 176 */ props.setProperty("python.executable", executablePath); /* */ /* 178 */ PythonInterpreter.initialize(System.getProperties(), props, argv); /* */ /* 180 */ //String frameworkDir = System.getProperty("java.ext.dirs"); /* 181 */ //File monkeyRunnerJar = new File(frameworkDir, "monkeyrunner.jar"); /* 182 */ //if (monkeyRunnerJar.canRead()) /* 183 */ // PySystemState.packageManager.addJar(monkeyRunnerJar.getAbsolutePath(), false); /* */ }
Example 3
Source File: JythonScriptSupport.java From phoebus with Eclipse Public License 1.0 | 4 votes |
/** Perform static, one-time initialization */ private static boolean init() { try { final Properties pre_props = System.getProperties(); final Properties props = new Properties(); // Jython 2.7(b3) needed these to set sys.prefix and sys.executable. // Locate the jython plugin for 'home' to allow use of /Lib in there // final String home = null; // getPluginPath("org.python.jython", "/"); // If left undefined, initialization of Lib/site.py fails with // posixpath.py", line 394, in normpath AttributeError: // 'NoneType' object has no attribute 'startswith' // props.setProperty("python.home", home); // props.setProperty("python.executable", "None"); // Disable cachedir to avoid creation of cachedir folder. // See http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html#java-package-scanning // and http://wiki.python.org/jython/PackageScanning props.setProperty(RegistryKey.PYTHON_CACHEDIR_SKIP, "true"); // By default, Jython compiler creates bytecode files xxx$py.class // adjacent to the *.py source file. // They are owned by the current user, which typically results in // problems for other users, who can either not read them, or not // write updates after *.py changes. // There is no way to have them be created in a different, per-user directory. // C Python honors an environment variable PYTHONDONTWRITEBYTECODE=true to // disable its bytecode files, but Jython only checks that in its command line launcher. // Use the same environment variable in case it's defined, // and default to disabled bytecode, i.e. the safe alternative. if (System.getenv("PYTHONDONTWRITEBYTECODE") == null) Options.dont_write_bytecode = true; else Options.dont_write_bytecode = Boolean.parseBoolean(System.getenv("PYTHONDONTWRITEBYTECODE")); // With python.home defined, there is no more // "ImportError: Cannot import site module and its dependencies: No module named site" // Skipping the site import still results in faster startup props.setProperty("python.import.site", "false"); // Prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0. props.setProperty("python.console.encoding", "UTF-8"); // This will replace entries found on JYTHONPATH final String python_path = Preferences.python_path; if (! python_path.isEmpty()) props.setProperty("python.path", python_path); // Options: error, warning, message (default), comment, debug // props.setProperty("python.verbose", "debug"); // org.python.core.Options.verbose = Py.DEBUG; PythonInterpreter.initialize(pre_props, props, new String[0]); final PySystemState state = Py.getSystemState(); final PyList paths = state.path; // Add the examples/connect2j to path. // During development, examples are in // "file:/some/path/phoebus/applications/display/model/target/classes/examples" final String examples = ModelPlugin.class.getResource("/examples").toString(); if (examples.startsWith("file:")) paths.add(examples.substring(5) + "/connect2j"); // In the compiled version, examples are in // "jar:file:/some/path/display-model-0.0.1.jar!/examples" else if (examples.startsWith("jar:file:")) paths.add(examples.substring(9).replace("jar!/", "jar/") + "/connect2j"); else logger.log(Level.WARNING, "Cannot locate examples/connect2j from " + examples); final PyVersionInfo version = PySystemState.version_info; logger.log(Level.INFO, "Initial Paths for Jython " + version.major + "." + version.minor + "." + version.micro + ": " + paths); // Scripts would sometimes fail in "from ... import ..." with this error: // // File "..jython-standalone-2.7.1.jar/Lib/warnings.py", line 226, in warn // IndexError: index out of range: 0 // // That version of Lib/warnings.py:226 tries to read sys.argv[0], // so setting sys.argv[0] avoids the crash. // Since state is shared by all scripts in a display, // set it to a generic "DisplayBuilderScript" state.argv.clear(); state.argv.add("DisplayBuilderScript"); return true; } catch (Exception ex) { logger.log(Level.SEVERE, "Once this worked OK, but now the Jython initialization failed. Don't you hate computers?", ex); } return false; }
Example 4
Source File: JythonSupport.java From phoebus with Eclipse Public License 1.0 | 4 votes |
/** Perform static, one-time initialization */ private static boolean init() { final List<String> paths = new ArrayList<>(); try { final Properties pre_props = System.getProperties(); final Properties props = new Properties(); // Compare jython setup with // org.csstudio.display.builder.runtime.script.internal.JythonScriptSupport // Disable cachedir to avoid creation of cachedir folder. // See http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html#java-package-scanning // and http://wiki.python.org/jython/PackageScanning props.setProperty(RegistryKey.PYTHON_CACHEDIR_SKIP, "true"); // By default, Jython compiler creates bytecode files xxx$py.class // adjacent to the *.py source file. // They are owned by the current user, which typically results in // problems for other users, who can either not read them, or not // write updates after *.py changes. Options.dont_write_bytecode = true; // With python.home defined, there is no more // "ImportError: Cannot import site module and its dependencies: No module named site" // Skipping the site import still results in faster startup props.setProperty("python.import.site", "false"); // Prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0. props.setProperty("python.console.encoding", "UTF-8"); // Options: error, warning, message (default), comment, debug // props.setProperty("python.verbose", "debug"); // No need to add numjy, // it's found on __pyclasspath__ because it's in the scan-model module // Add scan script paths for (String pref_path : ScanServerInstance.getScanConfig().getScriptPaths()) { // Resolve built-in examples if (pref_path.startsWith(PathStreamTool.EXAMPLES)) { String path = PathStreamTool.patchExamplePath(pref_path); final URL url = ScanServerInstance.class.getResource(path); if (url == null) throw new Exception("Error in scan script path " + pref_path); path = url.toExternalForm(); // Patch file:/path/to/the_file.jar!/path/within if (path.startsWith("file:")) path = path.substring(5); path = path.replace(".jar!", ".jar"); paths.add(path); } else // Add as-is paths.add(pref_path); } props.setProperty("python.path", paths.stream().collect(Collectors.joining(java.io.File.pathSeparator))); PythonInterpreter.initialize(pre_props, props, new String[0]); final PySystemState state = Py.getSystemState(); final PyVersionInfo version = PySystemState.version_info; logger.log(Level.INFO, "Initial Paths for Jython " + version.major + "." + version.minor + "." + version.micro + ":"); for (Object o : state.path) logger.log(Level.INFO, " * " + Objects.toString(o)); // Display Builder Scripts would sometimes fail in "from ... import ..." with this error: // // File "..jython-standalone-2.7.1.jar/Lib/warnings.py", line 226, in warn // IndexError: index out of range: 0 // // That version of Lib/warnings.py:226 tries to read sys.argv[0], // so setting sys.argv[0] avoids the crash. state.argv.clear(); state.argv.add("ScanServerScript"); } catch (Exception ex) { logger.log(Level.SEVERE, "Once this worked OK, but now the Jython initialization failed. Don't you hate computers?", ex); return false; } return true; }