Jupyter Notebook, NameError: is not defined, %%time prevents assignment

3k views Asked by At

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.

1

There are 1 answers

0
Claude COULOMBE On

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?