我正在尝试在Windows上运行jython servlets。我甚至不能运行最简单的 HelloWorld.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.24webappsjythonWEB-INFlib
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
我最基本的网络应用程序正在C:apache-tomcat-8.5.24webappsjython
jython.jar
(不是独立 jar(被复制为C:apache-tomcat-8.5.24webappsjythonWEB-INFlibjython.jar
我的web.xml
取自 http://www.jython.org/javadoc/org/python/util/PyServlet.html:
<!-- C:apache-tomcat-8.5.24webappsjythonWEB-INFweb.xml -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="3.1">
<servlet>
<servlet-name>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<init-param>
<param-name>python.home</param-name>
<param-value>C:\jython2.7.0</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</url-pattern>
</servlet-mapping>
</web-app>
错误消息说,sys.path
中包含错误的目录。 python.home
不是从我的初始化参数中获取的(为什么会这样?(,而是从jython.jar
的位置猜测的。
PyServlet
初始化
我看了一下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
初始化参数添加为属性,并且该属性用于构造 JavaDoc 中所示的sys.path
!
我错过了什么?
使用 jython-standalone.jar 对我有用。
In Maven:https://mvnrepository.com/artifact/org.python/jython-standalone
绒球.xml:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.1</version>
</dependency>
看起来 python 路径引用了默认位置。因此,您有两种选择来解决此问题。首先是将 Jython 库复制到 "C:\apache-tomcat-8.5.24\webapps\jython\WEB-INF\lib\Lib" 中。为了复制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-standalone.jar 添加到 python home,而不是提取的 Lib 文件夹。
p.setProperty("python.path", "PATH TO jython-standalone-2.7.0.jar");
或
JAVA_OPTS="$JAVA_OPTS -Dpython.home=/PATH TO /jython-standalone-2.7.0.jar"
希望这对某人有所帮助。
正如有人建议的那样。我使用了jython-独立并且效果很好。
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.2</version>
</dependency>
@Test
public void givenPythonScript_whenPythonProcessInvoked_thenSuccess() throws Exception {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(".\src\test\resources\hello.py");
}