'Jython - Reading PyCode object from file - ImportError?: No Module name pycode
I am using Jython to run Python code inside of my Java app. I am testing to see if I can compile some Python code to a PyCode object, serialize it, deserialize it, and then run it. Everything I've read suggests that I should be able to do this but I keep hitting an error I don't understand. I have attempted to create a smallest possible example to demonstrate what I'm doing (see code block below). I have included the output I'm receiving, as well.
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.python.core.PyCode;
import org.python.util.PythonInterpreter;
import org.python.util.PythonObjectInputStream;
public class Main {
public static void main(String[] args) throws Exception {
PythonInterpreter pi = new PythonInterpreter();
try {
PyCode code = pi.compile("print('compiled!')");
System.out.println("Code: %s".formatted(code));
pi.exec(code);
OutputStream os = Files.newOutputStream(Paths.get("compiled_code"), StandardOpenOption.CREATE);
System.out.println("os: %s".formatted(os));
ObjectOutputStream oos = new ObjectOutputStream(os);
System.out.println("oos: %s".formatted(oos));
oos.writeObject(code);
oos.close();
InputStream is = Files.newInputStream(Paths.get("compiled_code"), StandardOpenOption.READ);
System.out.println("is: %s".formatted(is));
ByteArrayInputStream bais = new ByteArrayInputStream(is.readAllBytes());
System.out.println("bais: %s".formatted(bais));
PythonObjectInputStream pois = new PythonObjectInputStream(bais);
System.out.println("pois: %s".formatted(pois));
code = (PyCode)pois.readObject();
System.out.println("Code: %s".formatted(code));
pois.close();
pi.exec(code);
}
}
}
This is the output. As you can see, it doesn't execute the second time and the error seems to be from within Python rather than from an IO issue. It seems to throw the exception from readObject()
Code: <code object <module> at 0x2, file "<script>", line 0>
compiled!
os: java.nio.channels.Channels$1@3bbf9027
oos: java.io.ObjectOutputStream@15e0fe05
is: sun.nio.ch.ChannelInputStream@41bf79da
bais: java.io.ByteArrayInputStream@1a891add
pois: org.python.util.PythonObjectInputStream@62a4417
Exception: ImportError: No module named pycode
A fair suggestion would be to load the .py source code file rather than the compiled object. In response, my tests have suggested that executing a PyCode object is significantly faster and I would prefer not to transmit the source files, both for code obfuscation and to avoid the end-user from having to compile at each runtime.
I'd be very grateful for any support that can be provided.
Solution 1:[1]
Acknowledged to not be working by internal developer. See https://github.com/jython/jython/issues/153
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ari Black |
