in my program I have undefined variable somewhere:
global name 'cmd' is not defined
How can I pull out the line number where the variable is being used?
I know where the e开发者_如何学运维rror is (cmd versus self.cmd). The question is how to get the line number (or why it isnt shown).
Since I was catching error, I had to traceback.print_exc()
to get line numbers
Where are you running your program? a Python stacktrace usually looks like this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'cmd' is not defined
As you can see it indicates the line where the error is happening.
The traceback that results from this NameError being raised contains the line number which attempts to use the name. In this particular case, it seems likely that you forgot to import the 'cmd' module, but it's also possible you had a variable that was coincidentally named after a standard library module.
精彩评论