ImportError:无法导入站点模块及其依赖项:没有名为si的模块

2024-09-28 23:41:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Windows上运行jythonservlets。我连最简单的都跑不了地狱世界.py. 我得到以下500个错误:

message Servlet.init() for servlet [PyServlet] threw exception
 ...
description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.
exception
javax.servlet.ServletException: Servlet.init() for servlet [PyServlet] threw exception
 ...
cause mère
ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
  * sys.path: ['C:\\apache-tomcat-8.5.24\\webapps\\jython\\WEB-INF\\lib\\Lib', '__classpath__', '__pyclasspath__/']
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: C:\apache-tomcat-8.5.24\webapps\jython\WEB-INF\lib
    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

    org.python.core.Py.ImportError(Py.java:328)
    org.python.core.Py.importSiteIfSelected(Py.java:1563)
    org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:116)
 ...

Jython部署在C:\jython2.7.0

我最基本的webapp在C:\apache-tomcat-8.5.24\webapps\jython

jython.jar(不是独立的jar)被复制为C:\apache-tomcat-8.5.24\webapps\jython\WEB-INF\lib\jython.jar

我的web.xml取自http://www.jython.org/javadoc/org/python/util/PyServlet.html

^{pr2}$

错误消息显示,sys.path中包含了错误的目录。^{my7>为什么不从初始化,它是从jython.jar的位置猜出来的。在

PyServlet初始化

我看了一下the initialization code,看了PyServlet

@Override
public void init() {
    Properties props = new Properties();
    // Config parameters
    Enumeration<?> e = getInitParameterNames();
    while (e.hasMoreElements()) {
        String name = (String)e.nextElement();
        props.put(name, getInitParameter(name));
    }
     // ...
        init(props, getServletContext());
    }
    reset();
}

/**
 * PyServlet's initialization can be performed as a ServletContextListener or as a regular
 * servlet, and this is the shared init code. If both initializations are used in a single
 * context, the system state initialization code only runs once.
 */
protected static void init(Properties props, ServletContext context) {
    String rootPath = getRootPath(context);
    context.setAttribute(INIT_ATTR, true);
    Properties baseProps = PySystemState.getBaseProperties();
    // Context parameters
    Enumeration<?> e = context.getInitParameterNames();
    while (e.hasMoreElements()) {
        String name = (String)e.nextElement();
        props.put(name, context.getInitParameter(name));
    }
    if (props.getProperty("python.home") == null
            && baseProps.getProperty("python.home") == null) {
        props.put("python.home", rootPath + "WEB-INF" + File.separator + "lib");
    }
    PySystemState.initialize(baseProps, props, new String[0]);
    // ...
    PySystemState.add_classdir(rootPath + "WEB-INF" + File.separator + "classes");
    PySystemState.add_extdir(rootPath + "WEB-INF" + File.separator + "lib", true);
}

我希望将python.home初始化参数添加为属性,并将该属性用于构造sys.pathas shown in the JavaDoc!在

我错过了什么?在


Tags: thenameorgwebhomestringinitlib
3条回答

看起来python路径引用了一个默认位置。所以你有两个选择来解决这个问题。首先是复制“C:\apache-tomcat-8.5.24\webapps\Jython\WEB-INF\lib”中的Jython库。为了复制jython库,您需要将jython独立jar的内容解压到这个文件夹中。在

另一个选项是将python路径设置为指向jython独立jar解压的位置。代码如下所示:

 PythonInterpreter interpreter = null;
    try{
            Properties p = new Properties();
            p.setProperty("python.path", "PATH OF JYTHON");
            p.setProperty("python.home", "PATH OF JYTHON");
            p.setProperty("python.prefix", "PATH OF JYTHON");
            PythonInterpreter.initialize(System.getProperties(), p, new String[] {});
        interpreter = new PythonInterpreter();
    }catch(Exception ex){
        log.error("Exception while creating python interpreter: "+ex.toString());
    }

我遇到了这个错误,上面的解决方案对我不起作用。 我们需要加上jython-独立.jar到python主目录而不是提取的Lib文件夹。在

p.setProperty("python.path", "PATH TO jython-standalone-2.7.0.jar");

或者

^{pr2}$

希望这对某人有帮助。在

使用jython-独立.jar对我有用。在

在Maven中:https://mvnrepository.com/artifact/org.python/jython-standalone

在pom.xml文件公司名称:

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.1</version>
</dependency>

相关问题 更多 >