1

I'm running into a strange situation simply running a script from the command-line: when I run into errors, often the traceback given contains code that makes no sense. Every line in the traceback should be a method call, but sometimes they aren't. Also, the lines referenced in the traceback don't correspond to the given error. What's happening? As an example, here is a simple error where numpy wasn't imported, but the traceback makes little sense and refers to unrelated code lines:

Traceback (most recent call last):
  File "bin/train_global_model.py", line 549, in <module>
    if __name__ == '__main__':
  File "bin/train_global_model.py", line 236, in main
    def main():
  File "bin/train_global_model.py", line 407, in do_training
    tb_writer=train_writer,
  File "bin/train_global_model.py", line 200, in run_iteration
    print(accuracy)
NameError: global name 'np' is not defined

Pay special attention to the code lines referenced.

Is python caching code that is executed somewhere but then referring to the actual file when it's tracing an exception? Running Python 2.7.13.

  • 1
    Can't diagnose what we can't reproduce. Post a [mcve]. – Arya McCarthy Jun 04 '17 at 06:35
  • I would, but I can't. It doesn't seem to be the code itself that is causing the issue. I'm hoping for some help with what to try in order to find out the cause. – Jeroen van de Ven Jun 04 '17 at 07:18
  • 1
    Check for *.py files in your local directory that duplicate another python library. I've been caught out numerous times by having a file called abc.py – Rolf of Saxony Jun 04 '17 at 07:18
  • Does this answer your question? [Why does error traceback show edited script instead of what actually ran?](https://stackoverflow.com/questions/55492150/why-does-error-traceback-show-edited-script-instead-of-what-actually-ran) – user202729 Nov 02 '22 at 04:25

1 Answers1

2

Python saves line numbers not the actual source code, when running programs. For tracebacks it loads the source code and shows the corresponding lines to the numbers. When the source changes, while the program is running, you get lines out of sync.

Daniel
  • 42,087
  • 4
  • 55
  • 81