尝试从另一个文件中定义的方法调用wsadmin对象会产生NameError

时间:2013-04-26 15:06:41

标签: websphere jython wsadmin

我有一个名为test.py的脚本,它是从def.py导入所有定义的主要脚本。

def.py

def test():
  print AdminApp.list() #Prints the applications installed
#EndDef

test.py

import sys
from def import *
test()

这会抛出一个NameError,指出无法将AdminApp对象标识为有效的函数,关键字或变量。

WASX7093I: Issuing message: "WASX7017E: Exception received while running file "test.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<string>", line 10, in ?
  File "/opt/home/echkaay/wsadmin/test.py", line 3, in ?
NameError: AdminApp 

任何方向?

2 个答案:

答案 0 :(得分:0)

developerWorks上wsadminlib.py的这个节有用吗?

# Provide access to wsadminlib methods when accessed as an import.
# This is benign if wsadminlib is opened with execfile().
# Supports both connected and disconnected operations.
# (ie, works when wsadmin is connected to a running server,
# and works when wsadmin is not connected to a server.)
try:
    AdminConfig = sys._getframe(1).f_locals['AdminConfig']
    AdminApp = sys._getframe(1).f_locals['AdminApp']
    AdminControl = sys._getframe(1).f_locals['AdminControl']
    AdminTask = sys._getframe(1).f_locals['AdminTask']
except:
    print "Warning: Caught exception accessing Admin objects. Continuing."

答案 1 :(得分:-1)

由于我在definitions.py脚本中对管理对象进行了全球化,为了使它们始终可用,而不是使用定义import *导入它们,我使用了exec文件。

execfile(scriptPath + "/jython/definitions.py")

这样做了。