Siyaram Malav
Siyaram Malav

Reputation: 4718

AttributeError: 'module' object has no attribute 'timeit' while doing timeit a python function

i want to timeit a python function or want to print best time it takes for executing my_function() for 12 iteration. below is my code:

def my_function():
    print "hello"

if __name__ == "__main__":
    import timeit
    setup = "from __main__ import my_function"
    print timeit.timeit("my_function()", setup=setup,number=12)

but i am getting below errror

Traceback (most recent call last):
  File "timeit.py", line 7, in <module>
    print timeit.timeit("my_function()", setup=setup,number=12)
AttributeError: 'module' object has no attribute 'timeit'

anybody please help..

Upvotes: 2

Views: 2747

Answers (1)

BrenBarn
BrenBarn

Reputation: 251568

You named your file timeit.py, which blocks the builtin module, so import timeit is importing your own file. Name your file something else.

Upvotes: 12

Related Questions