7

I am trying to calculate the timings of my python code but I keep getting:

TypeError - 'module' is not callable

import timeit
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)

The link I am checking is this - https://docs.python.org/2/library/timeit.html

I would expect to see the time that took the code to run.

5
  • Your code works fine for me in both python 2.75 and 3.6.8. Commented Oct 2, 2019 at 8:44
  • 2
    Check that you run exactly the code you've posted here. Commented Oct 2, 2019 at 8:46
  • I am using 3.6.8 as well, no idea what could be the reason to raise that error. Yes, I am using the exact code snippet as above. Commented Oct 2, 2019 at 8:47
  • 4
    It looks that you tried to execute "timeit()" instead of "timeit.timeit()" Commented Oct 2, 2019 at 8:47
  • your error is raised if you write only timeit instead of timeit.timeit Commented Oct 2, 2019 at 9:28

3 Answers 3

14

Did you perhaps name your file as timeit.py?

$ cat timeit.py
import timeit
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)

$ python3 timeit.py
Traceback (most recent call last):
  File "timeit.py", line 1, in <module>
    import timeit
  File "/path/to/timeit.py", line 2, in <module>
    timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
TypeError: 'module' object is not callable

You have to rename your file to something else (ex. mytimeit.py).
Otherwise import will import your timeit instead of the real timeit module.

You can check the import-ed module with print(timeit.__file__).

$ cat timeit.py
import timeit
print(timeit.__file__)

timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)

$ pwd
/work/scratch

$ python timeit.py
/work/scratch/timeit.py
Traceback (most recent call last)
...

Also, if you want to see "the time that took the code to run", you have to print it.

$ mv timeit.py mytimeit.py
$ cat mytimeit.py
import timeit
print(timeit.__file__)
print(timeit.timeit('"-".join(str(n) for n in range(100))', number=10000))

$ python3 mytimeit.py 
/usr/lib/python3.6/timeit.py
0.1469665479962714
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sir. Indeed I have named it timeit.py and the same issue was with stringio.py naming - will note that in to future, thanks again.
Thank you so much! I was getting furious as my script worked just fine whenever I ran it in Spyder but not when I called it via python timeit.py from the command line. Guess that's a life lesson to never ever give a file the same name as a module.
0

had a similar "module is not callable" error and my solution was simpler: My solution was:

from timeit import timeit

Comments

-1

Let's go!

import time

origin_time = time.time() with this you will get the current time in seconds from the epoch

And whenever you want to know the time spent, just write:

current_spent_time = time.time() - origin_timethis will get again the current time (in seconds) and substracts the previous origin_time which was the time when your code start running.

Hope this helps!!

2 Comments

Hi, so I would call origin_time in the first line after all of the import section, and call current_spent_time after specific code parts to get the time spent, neat. But I want to use the timeit library to check the time it took to run program 10,000 times.
What about for i in range(10000)and then append the time to a list and after sum(list)/10000

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.