是否可以在使用graalpython从Java运行的另一个脚本中导入本地python脚本?
在Java (Scala)方面,代码如下所示:
val context = Context.newBuilder("python").
allowAllAccess(true).
option("python.ForceImportSite", "true").
option("python.Executable", "pyScripts/venv/bin/graalpython").
build()
val source = Source.newBuilder("python", new File("pyScripts/common/MyPyScript.py")).build()
context.eval(source)
val clazz = context.getPolyglotBindings.getMember("MyPyScript")
val instance = clazz.newInstance()
val res = instance.as(classOf[PyScriptApi])然后在graalpython脚本中,我想这样做(这两个python文件都在common子目录中):
import java
import polyglot
from common.ScriptBase import ScriptBase
class MyPyScript(ScriptBase):
...但是,这在Scala方面出现了一个错误:
Exception in thread "main" ModuleNotFoundError: No module named 'common'我知道Scala代码可以计算这个文件,但是我希望脚本编写器能够将脚本分割成多个文件。
发布于 2022-06-24 09:43:34
如果在Java/Scala代码中的上下文中设置"python.PythonPath“选项,那么本地导入就能工作:
val context = Context.newBuilder("python")
.allowAllAccess(true)
.option("python.PythonPath", "pyScripts")
.option("python.ForceImportSite", "true")
.option("python.Executable", "pyScripts/venv/bin/graalpython")
.build()https://stackoverflow.com/questions/72718927
复制相似问题