From: Joern E. <Joe...@mi...> - 2001-07-25 13:32:17
|
Hi all, hi Robert <snip> >>> [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 I've done so and now my script looks like this: # ----------- begin ob script import java java.lang.System.getProperty("python.home") java.lang.System.getProperty("python.path") import sys sys.path.append("c:\\program files\\jython;c:\\program files\\jython\\lib") print "python.home: ",java.lang.System.getProperty("python.home") print "python.path: ",java.lang.System.getProperty("python.path") print "sys.path: ",sys.path print "sys.prefix: ",sys.prefix # Import support for strings import string # Get the value of the scalar node value = scriptinterface.getNodeValue(".1.3.6.1...") # Convert value to INT valueAsInt=string.atoi(value) # Add 1 to value valueAsInt=valueAsInt+1 # Set a limit of 8 for value if valueAsInt > 8: valueAsInt=1 # Convert valueAsInt back to string value=str(valueAsInt) # Write back (increased) value to node scriptinterface.updateValue(".1.3.6.1...",value,"CONST") # ----------- end ob script And this is how the log-file looks right after executing the script: <snip> ... python.home: None python.path: None sys.path: ['.\\jars\\Lib', 'c:\\program files\\jython;c:\\program files\\jython\\lib'] sys.prefix: .\jars\ Traceback (innermost last): File "D:\AdventNet\simulator\mibs\_behavior_scripts_\counter_with_import.py", line 14, in ? ImportError: no module named string Exception while executing the Python Script 'python.home' is not set, therefore 'registry' could not be read and 'python.path' isn't set accurately, right? 'sys.path' is correct - the '.\\jars\\Lib' is the path leading to the jars containing all the class-files of the AgentSimulator. The module 'string.py' actually is present at 'c:\program files\jython\lib'. Doesn't it indicate the path to the modules is missing? Does jython use sys.path as well as python.path? > 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. Yes, the import error happens when I try to import fpformat, too, but 'fpformat.py' actually is present in the Jython lib directory 'c:\program files\jython\lib\'. I guess it's because of the missing 'python.path'. > <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, "") Hmmm ... because this will delete the old path that I may need further!? > 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...") >> >> # 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...",value,"CONST") >> # ---------- end of noneworking script > > This script could be written as: > > value = int( scriptinterface.getNodeValue(".1.3.6.1...") ) > value = value > 255 and "1" or str(value + 1) > scriptinterface.updateValue(".1.3.6.1...",value,"CONST") > > read the second line as: if value + 1 > 256, return "1" for the > expression, else return value + 1 as a string. I know the script could be coded more compact but it's example-code for my colleagues so I left it more detailed for them. >> 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 :) PythonInterpreter.initialize in fact is present. Just look at the example below. 'inizialize' is a typo. It actually _is_ 'initialize'. Sorry. The disatvantage is that we don't know which changes the programers from AdventNet made. >> 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. <snip> > Cheers, > Robert Bye Joern |