At least for my application I have solved the problem partly.
If I am not using the ScriptEngine it works.

In my simple test I did the following:

List<String> pythonPath = new ArrayList<String>();
pythonPath.add("<my path to jython lib>");
pythonPath.add("<my path to jython lib>/site-packages");
       
PySystemState pySystemState = new PySystemState();
for(String path: pythonPath)
{
     pySystemState.path.append(Py.newString(path));
}
Py.setSystemState(pySystemState);
PythonInterpreter p = new PythonInterpreter();
p.execfile("C:/DATA/download/ply-3.3/example/calc/calc.py");


The only question is now why it does not work when using the ScriptEngine ...

Best regards
Markus



2009/10/7 Markus Krosche <markus.krosche@googlemail.com>
Hello together,

I figured out something and I want to know the difference between the following two approaches:
(1) I have installed jython and the lastest version of ply (http://www.dabeaz.com/ply/) by running jython.bat setup.py install (which comes with the ply package).
It simply installs the py files inside the 'site-packages' folder.

Then I run the 'calc.py' example which comes with the ply package.

It runs fine. Everything works.


(2) I use Jython in my Java software and I want to use the module lex and yacc (which are part of the ply package).

Here is my code:

public class JythonScriptEngineProvider
{
    public ScriptEngine createEngine(final List<String> pathList)
    {
        PySystemState pySystemState = new PySystemState();
        for(String path: pathList)
        {
            pySystemState.path.append(Py.newString(path));
        }
        Py.setSystemState(pySystemState);
        final ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
        return engine;
    }
}


The pathList argument can be used to set the python path. And I am using the above class as follows:

List<String> pythonPath = new ArrayList<String>();
pythonPath.add("<my path to the jython lib>");
pythonPath.add("<my path to the jython lib>/site-packages");

ScriptEngine scriptEngine = new JythonScriptEngineProvider().createEngine(pythonPath);
scriptEngine.eval("import sys");
scriptEngine.eval("print sys.path");
scriptEngine.eval("import ply.lex as lex");
scriptEngine.eval("print lex");
scriptEngine.eval("lex.lex()");

The same error will result when calling the 'calc.py' example like this:

final String pyFile = "C:/DATA/download/ply-3.3/example/calc/calc.py";
scriptEngine.eval(new FileReader(new File(pyFile)));

The error is:

  File "<script>", line 1, in <module>
  File "C:\DATA\spt_workspace\MEPOtrunk\res\python\site-packages\ply\lex.py", line 887, in lex
    ldict = get_caller_module_dict(2)
  File "C:\DATA\spt_workspace\MEPOtrunk\res\python\site-packages\ply\lex.py", line 436, in get_caller_module_dict
    ldict = f.f_globals.copy()
  AttributeError: 'scope' object has no attribute 'copy'


Where is my mistake? What is the problem inside the code. I guess I am doing something completely wrong. But I did not understand it.

Thank you very much
Markus