Atovange
Atovange

Reputation: 23

NameError: name Timer is not defined

I have a small python program written for python 2.7.3:

import time
def fun():
    print('Hi')
for i in range(3):
    Timer(i, fun).start()

When I run it, I get the error:

NameError: name 'Timer' is not defined

How can I find out which module supports this functionality?

Upvotes: 0

Views: 11009

Answers (2)

Andy
Andy

Reputation: 50540

You are looking for the timeit module. You can use your existing code by replacing your current import:

from timeit import Timer

Upvotes: 0

Reloader
Reloader

Reputation: 720

Timer is in the timeit module, not time. And to call it like you want to, you would have to from timeit import Timer, not just import timeit. If you just declare import timeit, then you would have to write timeit.Timer instead of Timer everywhere in the code.

Upvotes: 5

Related Questions