在eclipse中用java调用python报错 Exception in thread "main" ImportError: Cannot import site module and its dependencies

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

原文地址:https://www.cnblogs.com/wang-jx/p/10896307.html

时间: 2024-08-09 14:47:55

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

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

使用命令 java -jar springBoot.jar  启动项目,结果报错如下: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 37 at java.lang.String.substring(String.java:1963) at org.springframework.boot.loader.jar.JarFile.create

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

Exception in thread "main" java.net.UnknownHostException: file at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.Socket.connect(Socket.java:579

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

信息: Refreshing org[email protected]41cf53f9: startup date [Wed Apr 05 16:48:12 CST 2017]; root of context hierarchy 四月 05, 2017 4:48:12 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definiti

使用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.csdn.net/jinxfei/article/details/4714809 原文地址:https://www.cnblogs.com/BaleW/p/8668712.html

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 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId>

报错Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: javax/xml/bind/...

首先我的jdk是11.05的 这个是由于: 这个是 由于缺少了javax.xml.bind,在jdk10.0.1中没有包含这个包,所以我自己去网上下载了jdk 8,然后把jdk10.0.1换成jdk 8问题就解决了 网址:jdk下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 重新运行后就没有报错了,希望能解决大家的问题 但其实我个人认为最好的解决的方法是在项目加个 

myeclipse运行tomcat报错Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: PermGen space

将myeclipse所配置的tomcat的jdk进行设置:-Xms512m -Xmx512m -XX:MaxNewSize=512m -XX:MaxPermSize=512m,如下图:

dom4j使用xpath报异常 Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext

Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230 ) at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207) at org.dom4j.tree.Abstrac

解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)

当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误: public static void main(String [] args) throws IOException { ProcessBuilder builder = new ProcessBuilder(); Process process = builder.command("dir d:\\").start(); InputStream inputStream = process.g