2

I created a default Maven Java project and added the following dependency:

<dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.0</version> </dependency> 

Then I created a package src/main/java/mypkg and added this class:

package mypkg; import javax.script.*; class JythonMinimalTest { public static void main(String[] args) throws Exception { String engineName = "python"; ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName(engineName); if (engine == null) { System.err.println("ERROR: `" + engineName + "` not available."); System.err.println("Available engines: "); for (ScriptEngineFactory factory: manager.getEngineFactories()) { System.err.println(factory); System.err.println("names:"); for (String name: factory.getNames()) { System.err.println(" " + name); } } System.exit(999); } engine.eval("print('hello, world')"); } } 

When I run it using exec:java,

mvn exec:java -Dexec.mainClass=mypkg.JythonMinimalTest 

I get the following mysterious output:

ERROR: `python` not available. Available engines: org.python.jsr223.PyScriptEngineFactory@2b0e6c89 names: python jython jdk.nashorn.api.scripting.NashornScriptEngineFactory@46618cb8 names: nashorn Nashorn js JS JavaScript javascript ECMAScript ecmascript 

The manager returns null, but then in the next line lists python/jython among the available script engines.

Nashorn worked just fine in exactly the same way. What am I doing wrong with Jython?

6
  • What version of Java are you using? Commented Oct 15, 2018 at 22:22
  • 1
    stackoverflow.com/a/33025866/2834978 Commented Oct 15, 2018 at 22:23
  • @ElliottFrisch maven uses both 1.8: <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target>, the java itself is also 1.8.0. Commented Oct 15, 2018 at 22:26
  • What happens if you use jython-standalone as artifactId instead of jython (see comment by Luis Muñoz)? Commented Oct 16, 2018 at 15:18
  • @mzjn Indeed, with jython-standalone 2.7.1, I don't need any Option.loadSite=false hacks, it works as expected right away. Note that jython-standalone.jar takes 40MB instead of 30MB. This solution is mentioned in {the question linked by Andrew in {the question linked by Luis Muñoz}}: whats-the-difference-between-jython-standalone-2-7-0-jar-and-jython-2-7-0-jar, but it has no accepted answers... Commented Oct 16, 2018 at 17:17

3 Answers 3

9

Here is one workaround:

Add this at the top:

import org.python.core.Options; 

Before invoking getEngineByName, set

 Options.importSite = false; 

Then the engine is created without problems.

Got the solution from this conversation.


Another solution that works well (proposed by mzjn in the comments) is to replace the jython artifact by jython-standalone.

Sign up to request clarification or add additional context in comments.

Comments

1

You either need to disable importSite with python Options, as you already discovered. Or, set the python.home to where you have extracted jython. I have it extracted in my home folder under jython2.7.0; adding this,

System.setProperty("python.home", new File( System.getProperty("user.home"), "jython2.7.0").getPath() ); 

allows python to discover the correct PYPATH so it can build the python environment.

2 Comments

Forgive me this possibly dumb question, but what do you mean by "extract"? I didn't extract anything anywhere, I just added five lines to the POM and hoped that I can use the engine just like any other dependency. I'm afraid that I still don't understand what the importSite option does, and what site.py is good for. Will I get into trouble as soon as I try to go beyond print('hello') if site.py isn't imported? What difference does it make?
Install jython using the installer file from their website, it contains a Lib folder. That contains a bunch of python modules that it uses to form the base python system. So you can import io or import pickle etc.
0

One way to fix this is to set the env var jythonpath to include the specific Lib directory you need for your instance (and include the Lib\site-packages as well to avoid further issues with specific packages)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.