欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jython报错,亲测有效Exception in thread “main“ ImportError: Cannot import site module and its dependencies:

程序员文章站 2024-02-08 18:05:28
...

在idea中使用jython时出现以下错误
jython报错,亲测有效Exception in thread “main“ ImportError: Cannot import site module and its dependencies:
**

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: [‘D:\maven\maven-repository\org\python\jython\2.7.0\Lib’, ‘classpath’, ‘pyclasspath/’]
    This attribute might be including the wrong directories, such as from CPython
  • sys.prefix: D:\maven\maven-repository\org\python\jython\2.7.0
    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]);

第二个方法,在pom.xml中加入jython-standalone的依赖

        <dependency>
            <groupId>org.python</groupId>
            <artifactId>jython-standalone</artifactId>
            <version>2.7.1</version>
        </dependency>
相关标签: java python