From: Ihab A.B. A. <ih...@ah...> - 2001-10-22 01:32:39
|
Hi there, I'm having what must be a pretty simple newbie problem embedding Jython. I'm using Jython 2.1 Alpha 1. I can reproduce my problem by a 2-line change to the included "SimpleEmbeddedjava" example; when I add -- interp.exec("import java.lang"); interp.exec("print java.lang"); I get the error -- Exception in thread "main" Traceback (innermost last): File "<string>", line 1, in ? ImportError: no module named java What am I missing? What do I need to do? I tried to figure it out by reading the code of 'jython.java', since I can definitely "import java.lang" in an interactive console, but I couldn't. Thanks and peace, Ihab -- Ihab A.B. Awad <ih...@ah...> Center for Computational Genomics and Bioinformatics, University of Minnesota. http://www.cbc.umn.edu/~ihab/ |
From: Ihab A.B. A. <ih...@ah...> - 2001-10-22 02:10:39
|
Hi again, According to "Ihab A.B. Awad" on Sunday 21 October 2001 20:32: > > interp.exec("import java.lang"); > interp.exec("print java.lang"); > > I get the error -- > > Exception in thread "main" Traceback (innermost last): > File "<string>", line 1, in ? > ImportError: no module named java Based on some surfing, I realized that I need to supply more information. I am running on RedHat Linux RawHide 7.1.94, with the Sun Linux JDK 1.3.1_01. I can't find any way to turn off the JIT with this JVM; passing "-classic" to the JVM does not help. I'm running Java with the command -- java -cp .:/opt/java/jython-2.1a1/jython.jar SimpleEmbedded and, trying to add 'rt.jar' to the command line, as in -- java -cp .: /opt/java/jdk/jdk1.3.1_01-i386-Linux/jre/lib/rt.jar: /opt/java/jython-2.1a1/jython.jar SimpleEmbedded does not help. Peace, Ihab -- Ihab A.B. Awad <ih...@ah...> Center for Computational Genomics and Bioinformatics, University of Minnesota. http://www.cbc.umn.edu/~ihab/ |
From: Ihab A.B. A. <ih...@ah...> - 2001-10-22 02:32:49
|
Yet more re this ... sorry for the proliferation of msgs. (a) I realized that "no module named java" was because my programs were not specifying a "python.cachedir" property correctly. Fixing this has fixed the original problem. (b) Now, running inside my JSP application (which is where I _really_ want to use Jython), when I do -- import os dir(os) I get the error -- ImportError: no module named javaos Eek. So, now, does _that_ look familiar to anyone? Thanks as always to all and peace, Ihab -- Ihab A.B. Awad <ih...@ah...> Center for Computational Genomics and Bioinformatics, University of Minnesota. http://www.cbc.umn.edu/~ihab/ |
From: Robert W. B. <rb...@di...> - 2001-10-22 04:02:35
|
Hello Ihab, On Sun, 21 Oct 2001, Ihab A.B. Awad wrote: > > Yet more re this ... sorry for the proliferation of msgs. > > (a) I realized that "no module named java" was because my programs were > not specifying a "python.cachedir" property correctly. Fixing this has > fixed the original problem. Good. > (b) Now, running inside my JSP application (which is where I _really_ want > to use Jython), when I do -- > > import os > dir(os) > > I get the error -- > > ImportError: no module named javaos > > Eek. So, now, does _that_ look familiar to anyone? Yes, os requires the javaos module, and you get the ImportError when that module cannot be Located. Fix= make sure module is found. Check your python.home/sys.path vars. Use PythonInterpreter.initialize before instantiating the interpreter to set properties like python.home so modules can be found. The org.python.util.PyServlet code is the best example of setting properties and the initialize method. -robert |
From: Ihab A.B. A. <ih...@ah...> - 2001-10-22 17:18:38
|
Hi all, Thanks Robert Bill & Jim Adrig for the tips.... > Yes, os requires the javaos module, and you get the ImportError when that > module cannot be Located. Fix= make sure module is found. I've been trying, in every manner possible! Here's my code -- rootPath = servletContext.getRealPath("/"); if (!rootPath.endsWith(File.separator)) rootPath += File.separator; Properties p = getInitParametersAsProperties(); p.put( "python.cachedir", servletContext.getAttribute("javax.servlet.context.tempdir").toString()); PythonInterpreter.initialize(System.getProperties(), p, new String[0]); PySystemState pySys = Py.getSystemState(); pySys.add_classdir(rootPath + "WEB-INF" + File.separator + "classes"); pySys.add_extdir(rootPath + "WEB-INF" + File.separator + "lib"); pySys.path.append(new PyString(servletContext.getAttribute("python.home") + File.separator + "Lib")); pySys.path.append(new PyString(rootPath + "WEB-INF" + File.separator + "py")); pythonInterpreter = new PythonInterpreter(null, pySys); I think, based on looking at PyServlet.java, that this should get all my bases covered. However, I still get -- import os dir(os) Traceback (innermost last): File "", line 10, in ? ImportError: no module named javaos import os print os.listdir('/home/ihab') Traceback (innermost last): File "", line 2, in ? AttributeError: class 'org.python.modules.os' has no attribute 'listdir' This is all very confusing. Any help appreciated from anyone. Peace, Ihab -- Ihab A.B. Awad <ih...@ah...> Center for Computational Genomics and Bioinformatics, University of Minnesota. http://www.cbc.umn.edu/~ihab/ |
From: Ihab A.B. A. <ih...@ah...> - 2001-10-22 18:39:51
|
Hi all, More re this issue. I finally decided to use PyServlet verbatim, as a baseline test case. Here is the relevant part of my "web.xml" file -- <servlet> <servlet-name>PyServlet</servlet-name> <servlet-class>org.python.util.PyServlet</servlet-class> <init-param> <param-name>python.home</param-name> <param-value>/opt/java/jython-2.1a1</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>PyServlet</servlet-name> <url-pattern>*.py</url-pattern> </servlet-mapping> and here is a servlet in Jython -- from javax.servlet.http import HttpServlet import os class test(HttpServlet): def doGet(self, req, res): res.setContentType("text/html"); out = res.getOutputStream() print >>out, "Hello World, how are we?" print >>out, dir(os) print >>out, os.listdir('/home/ihab') out.close() return Now, when I invoke this, I get the following -- Hello World, how are we? ['__depends__', 'classDictInit'] A Servlet Exception Has Occurred Traceback (innermost last): File "/home/ihab/www-docs/logs/../bin/../webapps/omtdb/ ... edu/umn/genomics/omtdb/test.py", line 24, in doGet AttributeError: class 'org.python.modules.os' has no attribute 'listdir' so notice that dir(os) does not show any of the symbols that I would expect to find and, of course, invoking os.listdir() raises an exception. Thanks & peace, Ihab -- Ihab A.B. Awad <ih...@ah...> Center for Computational Genomics and Bioinformatics, University of Minnesota. http://www.cbc.umn.edu/~ihab/ |
From: Robert W. B. <rb...@di...> - 2001-10-22 19:07:06
|
On Mon, 22 Oct 2001, Ihab A.B. Awad wrote: > Hi all, > > Thanks Robert Bill & Jim Adrig for the tips.... > > > Yes, os requires the javaos module, and you get the ImportError when that > > module cannot be Located. Fix= make sure module is found. > > I've been trying, in every manner possible! Here's my code -- > > rootPath = servletContext.getRealPath("/"); > if (!rootPath.endsWith(File.separator)) rootPath += File.separator; > > Properties p = getInitParametersAsProperties(); > p.put( > "python.cachedir", > servletContext.getAttribute("javax.servlet.context.tempdir").toString()); Maybe try: p.put("python.home", "some/path/that/contains/jython's/Lib/dir") Finding the cachedir will not help jython locate its modules, but finding the correct python.home will. > PythonInterpreter.initialize(System.getProperties(), p, new String[0]); > > PySystemState pySys = Py.getSystemState(); > pySys.add_classdir(rootPath + "WEB-INF" + File.separator + "classes"); > pySys.add_extdir(rootPath + "WEB-INF" + File.separator + "lib"); > pySys.path.append(new PyString(servletContext.getAttribute("python.home") + > File.separator + "Lib")); Maybe try printing python.home to make sure it isn't empty. Actually, printing python.home and sys.path is a good test. > pySys.path.append(new PyString(rootPath + "WEB-INF" + File.separator + > "py")); Are the jython modules in WEB-INF/py/? If javaos.py is there, I'm stumped :( > pythonInterpreter = new PythonInterpreter(null, pySys); > > I think, based on looking at PyServlet.java, that this should get all my > bases covered. Print python.path and python.home to make sure they are correct. That's the easiest way to make sure bases are covered. I would not trust everything is covered until I got a confirmation that the sys.path actually did include the directory that javaos.py lives in. Best of luck. -robert > However, I still get -- > > import os > dir(os) > > Traceback (innermost last): File "", line 10, in ? ImportError: no > module named javaos > > import os > print os.listdir('/home/ihab') > > Traceback (innermost last): File "", line 2, in ? AttributeError: class > 'org.python.modules.os' has no attribute 'listdir' > > This is all very confusing. Any help appreciated from anyone. Peace, |
From: Ihab A.B. A. <ih...@ah...> - 2001-10-22 22:25:17
|
Hey Robert & everyone, My sincerest apologies. I was using -- /opt/jython-2.1a1/ when I should have been using -- /opt/java/jython-2.1a1/ because, um, well -- the latter, not the former, is where Jython is installed on my system!!!! So I fixed my paths and things work now. :) That said, I think I learned a lot from this experience about setting up the Jython environment that I wouldn't have otherwise, so all is well. Many thanks for all the help. Regards & peace, Ihab -- Ihab A.B. Awad <ih...@ah...> Center for Computational Genomics and Bioinformatics, University of Minnesota. http://www.cbc.umn.edu/~ihab/ |