最近做项目需要用java调用python,配置了jython后,运行了例子代码:

获得一个元组里面的元素:

  1. import org.python.util.PythonInterpreter;
  2. public class FirstJavaScript {
  3. public static void main(String args[]) {
  4.  
  5. PythonInterpreter interpreter = new PythonInterpreter();
  6. interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
  7. interpreter.exec("print days[1];");
  8.  
  9. }// main
  10. }

运行时报错:

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

我们只需要把代码改为以下这个例子就可以顺利执行了。

  1. Properties props = new Properties();
  2. props.put("python.home", "path to the Lib folder");
  3. props.put("python.console.encoding", "UTF-8");
  4. props.put("python.security.respectJavaAccessibility", "false");
  5. props.put("python.import.site", "false");
  6. Properties preprops = System.getProperties();
  7. PythonInterpreter.initialize(preprops, props, new String[0]);
  8.  
  9. PythonInterpreter interpreter = new PythonInterpreter();
  10. interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
  11. interpreter.exec("print days[1];");

执行结果是Tue

在eclipse中用java调用python报错 Exception in thread "main" ImportError: Cannot import site module and its dependencies的更多相关文章

  1. java使用类数组 报错Exception in thread "main" java.lang.NullPointerException

    源代码如下: Point[] points=new Point[n];//Point是一个类 for(int i=0;i<n;i++) { System.out.print("请输入x ...

  2. Spring Boot 启动报错 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 37

    使用命令 java -jar springBoot.jar  启动项目,结果报错如下: Exception at java.lang.String.substring(String.java:) at ...

  3. Idea运行时Scala报错Exception in thread "main" java.lang.NoSuchMethodError:com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V

    一.情况描述 使用idea +scala+spark,运行程序代码如下: package cn.idcast.hello import org.apache.spark.rdd.RDD import ...

  4. xom报错 Exception in thread "main" java.net.UnknownHostException: file

    Exception in thread "main" java.net.UnknownHostException: file at java.net.AbstractPlainSo ...

  5. 创建Sqoop作业,报错Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject

    WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P in ...

  6. 使用IntelliJ工具打包kotlin为bat文件运行报错 Exception in thread "main" java.lang.NoClassDefFoundError

    Exception in thread "main" java.lang.NoClassDefFoundError 这个很有可能是因为idea里的java版本与电脑上的java环境 ...

  7. MyBatis笔记----报错Exception in thread "main" org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ij34.model.UserMapper.selectUser

    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41cf53f9: startup ...

  8. Spring报错:Exception in thread "main" java.lang.IllegalArgumentException at org.springframework.asm.ClassReader.<init>(Unknown Source)

    简单搭建了一个Spring Maven工程就报错: 看到网上说是JDK 7 和 Spring3.x :JDK编译级别设置成1.7,仍然没有得到解决,采用版本为  3.2.0.RELEASE <b ...

  9. 使用Grizzy+Jersey搭建一个RESTful框架()报错Exception in thread "main" java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;

    报错的类涉及UriBuilder,我搜索类发现, 这个类存在于两个包中,我在baidu的时候,也有人提到是jar包冲突,我就删除了 这个依赖,问题解决了. 环境搭建过程请见地址https://blog ...

随机推荐

  1. js中的克隆

    1.如果克隆对象是基本类型,直接复制就可以 <script type="text/javascript"> var str1 = 'abc' var str2 = st ...

  2. 2018.11.9浪在ACM集训队第四次测试赛

    2018.11.9浪在ACM集训队第四次测试赛 整理人:朱远迪 A 生活大爆炸版 石头剪刀布           参考博客:[1] 刘凯 B 联合权值            参考博客: [1]田玉康 ...

  3. 【62.89%】【BZOJ 1072】[SCOI2007]排列perm

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1862  Solved: 1171 [Submit][Status][Discuss] Descri ...

  4. 【译】PEP 318--函数和方法的装饰器

    PEP原文 : https://www.python.org/dev/peps/pep-0318 PEP标题: Decorators for Functions and Methods PEP作者: ...

  5. flutter 安装环境 Mac

    1.这是我第二次mac 安装 flutter环境了,但是这种东西记不住.打开写一下 有用的东西 一. ls vi 等命令 command not found原因是因为环境变量的问题,编辑profile ...

  6. asp.net core 3.x 通用主机原理及使用

    一.前言 只是讲asp.net core 3.x通用主机的大致原理,这些东西是通过查看源码以及自己根据经验总结得来的,在文章中不会深入源码,因为个人觉得懂原理就晓得扩展点,后期碰到有需求的时候再仔细去 ...

  7. VS Code 解决 因为在此系统上禁止运行脚本

    vscode执行命令的 主要是由于没有权限执行脚本.开通权限就可以解决啦 在搜索框中输入:powerShell 选择管理员身份运行 输入命令行:set-ExecutionPolicy RemoteSi ...

  8. 让你彻底明白TCP三次握手,四次挥手

    今天我们来讲一下TCP的三次握手和四次挥手,先来张思维导图.  一.TCP是什么 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流 ...

  9. MacBook Pro 入手一年了,到底香不香?

    最近又有小伙伴问到底值不值得入手一台 MacBook Pro,松哥自己在 2018 年 10 月份的时候入手了一台,到现在为止,也用了一年多了,今天就来和小伙伴们聊一聊使用感受,至于到底值不值,需要大 ...

  10. 01_console 你真的了解吗,不曾了解过得console~

    对于 console 你只知道 console.log 吗? 那你就 out 啦!!! // 1. 显示信息 console.log('hello'); console.info('信息'); con ...