I know many of these questions exist but I can't seem to find one that works for the problem I am having.
I have the following:
def function():
try:
# function to create a table on hbase
except AlreadyExists, ae:
print "WARN: " + ae.message
when I call it from开发者_StackOverflow社区 another python script it gives me
NameError: global name 'AlreadyExists' is not defined
...but if I remove the def function()
and run it on its own, it works and doesn't complain about the global name.
I tried putting global AlreadyExists
and that didn't work. I also looked at this similar problem but I'm not sure how to apply it because it works on its own (without me having to import anything specific, but as soon as I wrap it in a function it fails).
Any suggestions?
Python only tries to access the name when an exception is thrown. When you ran the code outside of the function it probably didn't throw the exception and that's why you think it worked.
You need to import the AlreadyExists exception from wherever it lives.
change
from hbase import ttypes
...
except AlreadyExists, ae:
to
except ttypes.AlreadyExists, ae:
http://www.ros.org/doc/api/hbase/html/classhbase_1_1ttypes_1_1AlreadyExists.html
精彩评论