最近做项目需要用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

最新文章

  1. U盘容量变小解决办法
  2. 分享一些不错的sql语句
  3. PCurve - Curve on Surface
  4. C# Dictionary几种遍历方式
  5. 解决 ko mapping 数组无法添加新对象的问题
  6. POJ 2386 Lake Counting (水题,DFS)
  7. Magento 处理方法
  8. 自由树的计数 Labeled unrooted tree counting
  9. Android 通过开源框架AsyncHttpClient进行get和post请求
  10. 经过一年时间的沉淀 再次回首 TCP Socket服务器编程--转
  11. react-native 自己搭建热更新服务器
  12. 使用GraceNote Web API发展Mac发现音乐信息的应用
  13. 【转义字符】HTML 字符实体< &gt: &
  14. 从零开始学java (四)反射
  15. <c:forEach varStatus="status">中 varStatus的属性简介
  16. 操作系统环境变量LANG和NLS_LANG的关系
  17. PropertySheet
  18. HTTP和HTTPS的区别,以及各自的优缺点
  19. zookeeper事件监听
  20. TrinityCore3.3.5编译过程-官方指导-踩坑总结

热门文章

  1. yum安装错误:CRITICAL:yum.cli:Config Error: Error accessing file for config file:///home/linux/+
  2. POJ 2387 Til the Cows Come Home(最短路模板)
  3. Linux 内核热插拔事件产生
  4. ZR9.8普转提
  5. KAFKA报错:COMMIT CANNOT BE COMPLETED SINCE THE GROUP HAS ALREADY REBALANCED AND ASSIGNED THE PARTITIONS TO ANOTHER MEMBER
  6. mac笔记本安装Android sdk
  7. C++中常量成员函数的含义
  8. Oracle和可视化视图工具教程
  9. 记录安装Python第三方包“tesserocr”的方法和遇到的坑
  10. Spring Boot 整合 slf4j+log4j2 实现日志管理