Timeit, NameError: global name is not defined. But I didn't use a global variable
global-variables, nameerror, python, timeit
Solution
Try this:
t = timeit.Timer(stmt='pe1()', setup='from pe1m import pe1')
`timeit.Timer` object doesn't know about the namespace you're calling it in so it can't access the `pe1m` module that you imported.
The `setup` argument is a statement executed in the context of the timed statement, they share the same namespace so whatever you define there, will be accessible in `stmt`.
Problem
I'd like to measure the execution speed of the following code: ``` def pe1(): l = [] for i in range(1000): if i%3 == 0 or i%5 == 0: l.append(i) print sum(l) ``` I stored this code under pe1m.py . Now I'd like to test the speed of file with the python interpreter. I did: ``` import timeit import pe1m t = timeit.Timer(stmt = 'pe1m.pe1()') t.timeit() ``` but I get: ``` File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/timeit.py", line 195, in timeit timing = self.inner(it, self.timer) File "<timeit-src>", line 6, in inner NameError: global name 'pe1m' is not defined ``` But I don't have any global variables.