使用jython-standalone时,通过:
1 2 |
PySystemState pySystemState = Py.getSystemState(); pySystemState.path.add(path); |
添加了py文件所在位置后在IDE中Debug时一切正常,能够看到jyphon将对应的py文件编译为了class。但是,当把项目打成jar包时则会报错:

打成jar包后jython抛异常:Cannot import site module and its dependencies: No module named site
可见其中的sys.path是错误的,正常情况下应该指向的是jython-standalone中的Lib。由于打成jar包后,其解出的***.jar!/BOOT-INF/lib/下根本就没用jython的东西,因此是有问题的。同时,上述异常中也指出了可以使用pyhon.home来指定这个目录。因此,我们直接将jython-standalone解压出来,然后用代码指定路径:

指定jython.home
同时需要注意,py文件需要跟jar包放在一起,否则搜索py时会在jar包同级的目录下搜索,最终得到的大致结构如下:

放置py和jar的最终情况
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » 解决Spring Boot或打包后jython报错:Cannot import site module and its dependencies: No module named site