Anycorn
Anycorn

Reputation: 51545

Python, trace undefined variables

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 error 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

Upvotes: 0

Views: 811

Answers (2)

Juan Gomez
Juan Gomez

Reputation: 1518

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.

Upvotes: 4

jonesy
jonesy

Reputation: 3542

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.

Upvotes: 0

Related Questions