[英]ImportError: cannot import name Error
I have a very simple test function that I need to capture its execution time using 'timeit' module, but I get an error 我有一个非常简单的测试函数,我需要使用“ timeit”模块捕获其执行时间,但出现错误
The function: 功能:
import timeit
def test1():
l = []
for i in range(1000):
l = l + [i]
t1 = timeit.Timer("test1()", "from __main__ import test1")
print(t1.timeit(number=1000))
The Error: C:\\Python34\\lib\\timeit.py:186: in timeit timing = self.inner(it, self.timer) :3: in inner ???
错误:C:\\ Python34 \\ lib \\ timeit.py:186:在timetime计时= self.inner(it,self.timer):3:在内部??? E
Ë
ImportError: cannot import name 'test1' =========== 1 error in 0.03 seconds ==============ImportError:无法导入名称'test1'============ 1个错误在0.03秒内==============
Can you guys help me with a solution? 你们可以帮我解决问题吗?
I think there are couple of problems with your code. 我认为您的代码有几个问题。 First for all make sure you can import timeit.
首先请确保您可以导入timeit。 And you have it as a module.
并且您将其作为模块。 For this, you can just run:
为此,您可以运行:
python -m timeit '"-".join(str(n) for n in range(100))'
If it execute fine then you sure have the timeit module. 如果执行正常,那么您肯定具有timeit模块。
Now regarding your question. 现在关于您的问题。 I took the liberty to rewriting it in a cleaner way.
我自由地以一种更干净的方式重写了它。
import timeit
def append_list():
num_list = []
for i in range(1000):
num_list.append(i)
print(timeit.timeit(stmt=append_list, number=1000)) #number is the number of repetion of the operation, in this case, 1000
# you can also run
print(timeit.timeit(stmt=append_list, number=1))
Now, the above could will do what you wanted to do, ie calculate the amount of time required to append numbers from 1 to 1000 to the list. 现在,上面的代码可以完成您想做的事情,即计算将1到1000的数字附加到列表所需的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.