From: Robert W. B. <rb...@di...> - 2001-07-24 17:32:32
|
Hello Joern, On Tue, 24 Jul 2001, Joern Eckhoff wrote: > > [Robert] > > Hello Joern, > > > > I only had two seconds free, but appended some ideas in case they > > help. Sorry for any gaps due to my rush. > > >>>> [Joern] > >>>> > >>>> While working in a DOS-Box import works really fine. But writing > >>>> a script which imports the same pakages throws an exception. > > >>> [finn] > >>> > >>> It sounds like a embedding problem. Try to check the value of sys.path > >>> at the beginning of your script. > >>> > >>> If you are doing the embedding take a look at this FAQ answer for how > >>> to set up a usefull python.path. > >>> > >>> http://www.jython.org/cgi-bin/faqw.py?req=show&file=faq06.002.htp > >>> > >>> Otherwise you can always add entries to sys.path from within your > >>> script. > > [Robert] > > 1. is python.home set so that it actually finds and reads the registry? > > [Joern] > > I don't know. How do I find out? The whole Jython-related code is "hidden" > within a jar-file. No chance to dip into it. >>> import java >>> java.lang.System.getProperty("python.home") Also add... >>> import sys >>> print sys.path, sys.prefix Knowing these three values is the best first step Is fpformat is the import error you originally saw? if so, it may be that you need to copy that module from the CPython distribution into your Jython Lib directory. <snip> > # ---------- start of noneworking script > from org.python.util import PythonInterpreter > from java.util import Properties > from java.lang import System re-initializing is probably the wrong way to go here. If you are running something in Jython and need to import modules from a specific directory, it is better to use: >>> import sys >>> sys.path.append("\\the\\required\\directory") instead of: > props = Properties() > props.setProperty("python.path", "c:\Program Files\Jython;c:\Program > Files\Jython\lib") > PythonInterpreter.initialize(System.getProperties(), props, "") Also make sure that the module does in fact live in one of the directories in sys.path. Side note: The 3rd arg to initialize should be [""] - it needs to convert to a String[] rather than just a String. However, the error message indicates other problems... > # Import support for strings > import string > > # Get the value of the scalar node > value = scriptinterface.getNodeValue(".1.3.6.1.4.1.781.5.8.1.1.1") > > # Convert value to INT > valueAsInt=string.atoi(value) > > # Add 1 to value > valueAsInt=valueAsInt+1 > > # Set limit for value to 256. If reached reset value to 1. > if valueAsInt > 256: > valueAsInt=1 > > # Convert valueAsInt back to string > value=str(valueAsInt) > > # Write back (increased) value to node > scriptinterface.updateValue(".1.3.6.1.4.1.781.5.8.1.1.1",value,"CONST") > # ---------- end of noneworking script This script could be written as: value = int( scriptinterface.getNodeValue(".1.3.6.1.4.1.781.5.8.1.1.1") ) value = value > 255 and "1" or str(value + 1) scriptinterface.updateValue(".1.3.6.1.4.1.781.5.8.1.1.1",value,"CONST") read the second line as: if value + 1 > 256, return "1" for the expression, else return value + 1 as a string. > The scripts were executed by the AgentSimulator at runtime whereat the > condition the script will be started can be chosen freely (requestBased, > timerBased, thresholdBased). How, why and were the script will be started > can't be seen at all - the whole jython-stuff is "hidden" in a jar. The > AgentSimulators' logfile contains this after executing the (noneworking) > script: > > Traceback (innermost last): > File "D:\AdventNet\simulator\mibs\_behavior_scripts_\counter.py", line 7, > in ? > AttributeError: class 'org.python.util.PythonInterpreter' has no attribute > 'initialize' > Exception while executing the Python Script My guess is that the error message above indicates initialize has been modified/renamed. No matter, I don't think it is what you are after anyway. > I'm wondering how this can happen, because using System.getProperties() is > absolutely similar to using PythonInterpreter.initialize() or am I wrong > somehow? Initialize is all about establishing system state information from system properties, registry properties and the new properties specified in the 2nd arg. So it is not really like getProperties. If jython is already running, however, intialize is trying to re-initialize things, which isn't really effective. > And 'initialize' isn't an attribute but a method. In the Python world, anything in a class is an attribute. The error message you see is a Jython error- not a Java error, so it is the correct error message to report only if "initialize" truly does not exist in your PythonInterpreter class. Check dir(PythonInterpreter) a few times to see if "initialize" is really there, or if it has been modified. Note that this is only to explain the error, I still don't think you want initialize for your situation :) > From my point of > view this should work fine, but as you can see it doesn't. It looks like > the path to the libs can't be found as before. I've set the correct path > within the file 'registry'. Using a DOS-Box behaves the same way: > > (Inside the DOS-Box, again.) > C:\>jython > Jython 2.0 on java1.3.0 (JIT: null) > Type "copyrigth", ... etc. > >>> from org.python.util import PythonInterpreter > >>> PythonInterpreter.initialize("old", "new", "") > Traceback (innermost last): > File "<console>", line 1, in ? > AttributeError: class 'org.python.util.PythonInterpreter' has no attribute > 'initialize' > >>> dir(PythonInterpreter) > ['err', 'eval', 'exec', 'execfile', 'get', 'getLocale', 'inizialize', > 'locals', 'out', 'set', 'setErr', 'setLocals', 'setOut'] Is 'inizialize' a typo in the mail, or does the dir really and truly report 'inizialize'. If it really and truly says 'inizialize', your results make more sense. <snip> > May jython has to be started with the -D option? "has to"? - no. You may use the -D option to start java applications that embed Jython. This allows the setting of properties from the command-line. Look at the jython.bat file for example. If your embedded Jython application that is giving you trouble implements initialize like org.python.util.jython does, you may be able to force certain properties with -D. For example: java -cp \path\to\jython.jar -Dpython.hom="c:\my\Jython\home\dir" \ -Dpython.path="c:\some\dir" org.python.util.jython This starts Jython with a specific python.home and python.path. Other Java apps that embed Jython and use the PythonInterpreter.initialize(System.getProperties(), props, new String[0]) should also make use of the -D options (unless they specifically override these options in the props values internally). My thought was that the application you are working with could merely be started with a -Dpython.path="\\some\\path" to add the path you desired to sys.path. But I misunderstood the situation. > .. *truncateMUCH* ... Ok, gimme a minute to figure out ... > > (Starting DOS-Box.) > C:\>jython > Jython 2.0 on java1.3.0 (JIT: null) > Type "copyrigth", ... etc. > >>> from org.python.util import PythonInterpreter > >>> inter = PythonInterpreter > >>> inter.initialize("old", "new", "") > Traceback (innermost last): > File "<console>", line 1, in ? > TypeError: initialize(): 1st arg can't be coerced to java.util.Properties > >>> _ > > .. this! Erm, bingo?! Let me have a looong and close look to that and > await my report on this topic soon. There's nothing wrong with using an interpreter object from within Jython, but the initialize is suspect, and again, probably not what you really wanted. > And last but not least a hopingly simple question. Why are so much people > using two single quotes instead of a quotation mark (like ''this'') or > different quotes (like `this') instead of two identical ones to distinguish > some kind of text (e.g. strings)? I'm wondering every time I see it. I'm not sure why others do. I use ` and ' because they look nice like smart quotes, and use ''this'' because of habitual avoidance of embedded double-quotes in Java strings. Cheers, Robert |