使用GraalVM从Java春季启动中调用python方法,简单的def起作用了,但是无法检测到另一个文件的导入def。收到错误消息“ 嵌套的异常是org.graalvm.polyglot.PolyglotException:ModuleNotFoundError:没有名为'train '的模块”“
#python code file 1
from file2 import train
def training(videoFileName,userId):
k = train(videoFileName,userId)
return (flag)
#python code file 2
import cv2
def train(videoFileName,userId):
# some code
# *****************
return (true)
#Java spring boot code
@Bean
public Context cittaFacePython() throws IOException {
Context ctx = Context.newBuilder("python").allowAllAccess(true).build();
File file = new File(pythonProperties.getFile());
Source source = Source.newBuilder("python", file).build();
ctx.eval(source);
return ctx;
}
Value value = cittaFacePython.eval("python", "training('" + videoFileName + "'," + userId + ")");
System.out.println("+++++++++++++++++++" + value.asString());
原因:org.graalvm.polyglot.PolyglotException:ModuleNotFoundError:没有名为“ train”的模块
at org.graalvm.polyglot.Context.eval(Context.java:341) ~[na:na]
at com.uvionicstech.uaaservice.facerecognition.config.PythonConfig.cittaFacePython(PythonConfig.java:25) ~[classes/:na]
at com.uvionicstech.uaaservice.facerecognition.config.PythonConfig$$EnhancerBySpringCGLIB$$d62b55fa.CGLIB$cittaFacePython$0(<generated>) ~[classes/:na]
at com.uvionicstech.uaaservice.facerecognition.config.PythonConfig$$EnhancerBySpringCGLIB$$d62b55fa$$FastClassBySpringCGLIB$$2cc6e395.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at com.uvionicstech.uaaservice.facerecognition.config.PythonConfig$$EnhancerBySpringCGLIB$$d62b55fa.cittaFacePython(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]