在eclipse中用java调用python报错 Exception in thread "main" ImportError: Cannot import site module and its dependencies
2024-08-29 19:37:39
最近做项目需要用java调用python,配置了jython后,运行了例子代码:
获得一个元组里面的元素:
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) { PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];"); }// main
}
运行时报错:
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
* sys.path: ['C:\\(你的项目地址)', '__classpath__', '__pyclasspath__/']
This attribute might be including the wrong directories, such as from CPython
* sys.prefix: C:(你的项目地址)
This attribute is set by the system property python.home, although it can
be often automatically determined by the location of the Jython jar file
You can use the -S option or python.import.site=false to not import the site module
我们只需要把代码改为以下这个例子就可以顺利执行了。
Properties props = new Properties();
props.put("python.home", "path to the Lib folder");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
执行结果是Tue
最新文章
- U盘容量变小解决办法
- 分享一些不错的sql语句
- PCurve - Curve on Surface
- C# Dictionary几种遍历方式
- 解决 ko mapping 数组无法添加新对象的问题
- POJ 2386 Lake Counting (水题,DFS)
- Magento 处理方法
- 自由树的计数 Labeled unrooted tree counting
- Android 通过开源框架AsyncHttpClient进行get和post请求
- 经过一年时间的沉淀 再次回首 TCP Socket服务器编程--转
- react-native 自己搭建热更新服务器
- 使用GraceNote Web API发展Mac发现音乐信息的应用
- 【转义字符】HTML 字符实体&;lt; &;gt: &;amp;
- 从零开始学java (四)反射
- <;c:forEach varStatus=";status";>;中 varStatus的属性简介
- 操作系统环境变量LANG和NLS_LANG的关系
- PropertySheet
- HTTP和HTTPS的区别,以及各自的优缺点
- zookeeper事件监听
- TrinityCore3.3.5编译过程-官方指导-踩坑总结
热门文章
- yum安装错误:CRITICAL:yum.cli:Config Error: Error accessing file for config file:///home/linux/+
- POJ 2387 Til the Cows Come Home(最短路模板)
- Linux 内核热插拔事件产生
- ZR9.8普转提
- KAFKA报错:COMMIT CANNOT BE COMPLETED SINCE THE GROUP HAS ALREADY REBALANCED AND ASSIGNED THE PARTITIONS TO ANOTHER MEMBER
- mac笔记本安装Android sdk
- C++中常量成员函数的含义
- Oracle和可视化视图工具教程
- 记录安装Python第三方包“tesserocr”的方法和遇到的坑
- Spring Boot 整合 slf4j+log4j2 实现日志管理