I can take an application with the following code in it:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import os");
interpreter.exec("import mylib");
Where the following is resources/Lib/mylib/__init__.py
:
from __future__ import print_function
from . import myfriend as thing
import os
print("Yep, everything works")
and compile it using maven, producing a my-app-with-dependencies.jar
I can easily run it with java -jar my-app-with-depenendencies.jar
and it works just fine, hooray!
Here's where the sad part comes in. I can put this exact same code inside a Spring handler:
@RequestMapping("/doesnotwork")
public @ResponseBody String sadness() {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import os");
interpreter.exec("import mylib");
return "Quoth the Java, nevermore";
}
and magically this no longer works. Not one little bit.
I can however move my file from resources/Lib/
to webapp/WEB-INF/lib/Lib/
and import mylib
works. But within mylib
I can no longer import from __future__
or os
. I can import sys
, and my sys.path looks like this:
['/path/to/my/webapp/WEB-INF/lib/Lib', '__classpath__', '__pyclasspath__/']
My sys.path_importer_cache
looks like this:
{'__classpath__': <type 'org.python.core.JavaImporter'>,
'/path/to/my/webapp/WEB-INF/lib/Lib': None,
'/path/to/my/webapp/WEB-INF/lib/Lib/mylib': None,
'__pyclasspath__/': <ClasspathPyImporter object at 0x2>}
What am I doing wrong that I can't import the stdlib? /path/to/my/webapp/WEB-INF/lib
contains both jython-2.7-b1.jar
and jython-standalone-2.7-b1.jar
. I've even tried inserting those jar files into my path and still no dice.
I can import java classes from .jar files present in the folder, except for ones found in the jython .jars. For instance, inside jython-2.7-b1.jar
are org/python/apache/xml/serialize/Serializer.class
. I can import org.python
but there only exists org.python.__name__
.