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.

2
  • 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. Commented Jun 19, 2019 at 6:06
  • @hpaulj I agree with you, but not everyone has your knowledge and understanding of iPython Notebook and they deserve to be helped anyway. Commented Jan 13, 2020 at 0:02

1 Answer 1

3

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.