From: Robert W. B. <rb...@di...> - 2001-07-25 19:19:09
|
On Wed, 25 Jul 2001, Joern Eckhoff wrote: > 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") This append may be the culprit. Sorry if I've missed this in earlier mails, but for... sys.path.append("c:\\;c:\\windows\\desktop") import test # a Jython script on my desktop The above does not work. sys.path is a list of individual directories, rather than system directory representations (e.g. ["\\path\\one", "\\path\\two"] instead of \\path\\one;\\path\\two). Instead, if I use: sys.path.append("c:\\") sys.path.append("c:\\windows\\desktop") import test # a Jython script on my desktop All is well. So, give this a try: sys.path.append("c:\\program files\\jython\\Lib") # only one dir # or sys.path.extend( ("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? yes, but these are not required. The right path in sys.path is the goal, these properties help the initialization step meet that goal. > 'sys.path' is correct Maybe not. > - 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? Properties, like python.path, python.home and etcettera, are all about initializing Jython. Java code takes steps to set Jython's system state before actually creating the interpreter instance, and these property values are what is used in these steps. The main step is PythonInterpreter.initialize(), but remember that this method should be called before instantiating an interpreter. After instantiation, Jython's sys module as the final word. python.path is added to the sys.path during initialization. Following that, what is in sys.path is the final word. > > >>> 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!? It's more because initialize is a bit out of place within Jython. Initialize should only be called one time- preceeding interpreter instantiations. This means called from Java. > 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. They probably didn't change anything. I was just looking for an easy explaination :) Cheers, Robert |