I have found a workaround...but it is very ugly.

I take all the contents of the Lib folder (e.g. datetime.py, etc) and copy them as resources into one of our JARs, so that Maven bundles them.

With that Jython is able to find them for imports...but for some reason is not able to find it when it is in the jython-standalone.jar itself,
while running in a One-Jar classloader.

If anyone can suggest a better (and more maintainable) solution it would be greatly appreciated

Jacek

On Wed, May 9, 2012 at 9:54 AM, Jacek Furmankiewicz <jacek99@gmail.com> wrote:
Hi,

We have a large Java Spring web app that we've converted to the One-Jar model (with embedded Jetty as the web server).
It integrates Jython to provide support for customer-specific code in particular parts of the application
which we initialize like this:

@PostConstruct
public void init() {
Properties props = new Properties();
props.setProperty("python.path", "/opt/software/custom");
PythonInterpreter.initialize(System.getProperties(), props, new String[] { "" });
interpreter = new PythonInterpreter();
}

with Jython scripts deployed into the /opt/software/custom folder where we pick them up and execute them from the parent Java logic to allow for customer-specific customizations.

Once we converted to the One-Jar model, everything else works...except particular Jython scripts.
Namely those that have an import statement, e.g.:

from datetime import datetime

When Jython encounters a script with any import in it it throws:

org.python.core.PyException: null
at org.python.core.Py.ImportError(Py.java:290) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.import_first(imp.java:750) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.import_name(imp.java:834) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importName(imp.java:884) ~[jython-full-2.5.2.jar:na]
at org.python.core.ImportFunction.__call__(__builtin__.java:1220) ~[jython-full-2.5.2.jar:na]
at org.python.core.PyObject.__call__(PyObject.java:357) ~[jython-full-2.5.2.jar:na]
at org.python.core.__builtin__.__import__(__builtin__.java:1173) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importFromAs(imp.java:978) ~[jython-full-2.5.2.jar:na]
at org.python.core.imp.importFrom(imp.java:954) ~[jython-full-2.5.2.jar:na]

if a script does not have an import statement (some of the very simple ones), they work just fine.

I've seen someone had the same issue, as mentioned in the EnigmaCurry blog:

http://www.enigmacurry.com/2009/05/20/distributing-jython-apps-in-a-single-jar-file/

See comment at the very bottom

Nice article! However, when i tried to use it for my app, I found that it was unable to find the Python libraries in the jar. On my development machine, they were found ok, but when I deployed to a machine which has neither Python nor Jython installed, it failed to load datetime. To make sure it wasn't some peculiarity of my app, I tweaked the provided sample app, adding 'import datetime' to the __init__.py. This gave the same failure. I suspect the issue is that Jython loads Python files using an explicit classloader, and therefore bypasses the one-jar classloader, but I haven't had time or inclination to verify this. 

If anyone could point us into what we need to change in the way we initialize the PythonInterpreter to make it support imports in a One-Jar environment, it would be greatly appreciated...the Definiteive Guide to Jython book mentions it as working out of the box, so I presume this is some recent regression.

Much appreciated

Jacek