4

I came across a very strange bug running a Jupyter Notebook (IPython: 7.4.0) where a variable was not assigned as normally. It took me quite bit of time to figure out the cause, searching in vain all over, variable scope, type conversion and TensorFlow intricacies ;(

In fact, using %%time cell magic was preventing assignment of the variable in the cell. Therefore the assigned variable was not defined in the cell below giving a characteristic error message: "NameError: 'xxx' is not defined."

It seems to be a known issue, hoping that can help someone else.

Claude COULOMBE
  • 3,434
  • 2
  • 36
  • 39
  • 1
    I'm a little puzzled by the issue discussion. I thought it was normal that variables assigned in a `timeit block, line or cell, would be local. I don't want the results of a 1000 iterations of some 'append' action to contaminate my main workspace. But then I'm looking at this as a console user, not a notebook one. – hpaulj Jun 19 '19 at 06:06
  • @hpaulj I agree with you, but not everyone has your knowledge and understanding of iPython Notebook and they deserve to be helped anyway. – Claude COULOMBE Jan 13 '20 at 00:02

1 Answers1

2

The solution is simple, just remove %%time from the cell.

Rather use:

from timeit import default_timer as timer
from datetime import timedelta

start = timer()

# Process
# ...


end = timer()
print ("Execution time HH:MM:SS:",timedelta(seconds=end-start))

Source: Stackoverflow - Measure time elapsed in Python?

Claude COULOMBE
  • 3,434
  • 2
  • 36
  • 39