From: Robert W. B. <rb...@di...> - 2001-08-25 00:17:20
|
Hellow Mike, On Fri, 24 Aug 2001, Krajnak, Mike (MED) wrote: > Summary: When I split my classes into multiple > files the JythonServlet reports a type error and > can't find the extra classes. > > Details: > I've set up and run the JythonServlet on my Win2K > box and created some sample servlets. I'm using > Jython 2.1a3. > > I have jython in D:\opt\jython and my servlet context > for Tomcat is at D:\dev\jyserv. The jyserv directory > looks like this: > > \jyserv > \WEB-INF > \classes > \lib > jython.jar > \jython > \sample > test1.py > > And test1.py is: > > --- jyserv/sample/test1.py: > import sys > from javax.servlet.http import HttpServlet > > class message: > def __init__(self, pMsg): > self.msg = pMsg > > def getMessage(self): > return self.msg > > class test1(HttpServlet): > def doGet(self, request, response): > response.setContentType("text/html") > out = response.getOutputStream() > > self.mesg = message("Hi Bob!") > out.println(self.mesg.getMessage()) > > out.close() > return > ---eof > > Running jython servlet in Tomcat and going to > http://localhost:8080/jyserv/sample/test1.py > > Displays the "Hi Bob!" message as expected. If I > split the files into two like so: > > --- jyserv\sample\message.py: > class message: > def __init__(self, pMsg): > self.msg = pMsg > > def getMessage(self): > return self.msg > ---eof > > --- jyserv\sample\test1.py: > import sys > from javax.servlet.http import HttpServlet > > class test1(HttpServlet): > def doGet(self, request, response): > response.setContentType("text/html") > out = response.getOutputStream() > > self.mesg = message("Hi Bob!") > out.println(self.mesg.getMessage()) > > out.close() > return > ---eof > > And after restarting Tomcat I get a type error: > > Traceback (innermost last): > File "D:\dev\jythonservlet\worklist\test.py", line 9, in doGet > TypeError: call of non-function (module 'message') > > I've tried various forms of import and from. I've inspected > python path to make sure \dev\jyserv was in the path. I'm fairly > new to Python. Has anyone else had this problem? Why can't I > split up my classes into different files? The import statement that imports "message" is missing from the test1.py example, so I cannot say for sure, but this usually happens when Java habits mix up Jython imports :) in other words, the difference between jython's package/module/class hierarchy as opposed to Java's package/class. If you used "import message" rather than "from message import message", you are calling the module when you do "self.mesg=message("Hi Bob!")". However, Jython doesn't let you call a module- thus the TypeError: call of non-function (module 'message'). Remember, your message class is in the message module, so references must be either: import message self.message = message.message("Hi Bob!") # BTW, Hi! OR from message import message self.message = message("Hi Bob!") cheers, Robert |