ImportError: cannot import name ‘clock‘ from ‘time‘ (unknown location),已解决

简介: ImportError: cannot import name ‘clock‘ from ‘time‘ (unknown location),已解决

ImportError: cannot import name ‘clock‘ from ‘time‘ (unknown location),解决办法


在采用蒙特卡罗方法求解π值时,出现上述报错,这是因为time.clock()在3.3版本后已移除,可使用**perf_counter()或者process_time()**代替。

bbc5026d4d374fdfad3c42658d6a7c48 (1).png

代替后:

from time import process_time as timer

修改后:

from random import random
from math import sqrt
from time import process_time as timer
DARTS = 1000
hits = 0.0
timer()
for i in range(1, DARTS+1):
    x, y = random(), random()
    dist = sqrt(x ** 2 + y ** 2)
    if dist <= 1.0:
        hits = hits + 1
pi1 = 4 * (hits/DARTS)
print("Pi值是{}.".format(pi1))


相关文章
Failed to execute script ‘xxx‘ due to unhandled exception:No module named ‘ctypes‘
Failed to execute script ‘xxx‘ due to unhandled exception:No module named ‘ctypes‘
473 0
|
4月前
|
JavaScript Python
7-4|execjs._exceptions.ProgramError: Error: Cannot find module 'jsdom'
7-4|execjs._exceptions.ProgramError: Error: Cannot find module 'jsdom'
|
8月前
|
编解码 算法 Python
ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’
ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’
100 0
|
数据库
解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matching
解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matching
1272 0
解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matching
|
JavaScript
Uncaught runtime errors: × ERROR Avoided redundant navigation to current location: “/xxx“.
Uncaught runtime errors: × ERROR Avoided redundant navigation to current location: “/xxx“.
220 0
go time.After() time.Ticker
go time.After() time.Ticker
42 0
|
TensorFlow 算法框架/工具 Python
成功解决File "frozen importlib._bootstrap", line 219, in _call_with_frames_removed ImportError: DLL lo
成功解决File "frozen importlib._bootstrap", line 219, in _call_with_frames_removed ImportError: DLL lo
成功解决File "frozen importlib._bootstrap", line 219, in _call_with_frames_removed ImportError: DLL lo
成功解决absl.flags._exceptions.UnrecognizedFlagError: Unknown command line flag 'data_format'
成功解决absl.flags._exceptions.UnrecognizedFlagError: Unknown command line flag 'data_format'
|
NoSQL Python
解决gdb报错:Failed to import the site module,No module named '_sysconfigdata_m'
解决gdb报错:Failed to import the site module,No module named '_sysconfigdata_m'
407 0
|
Python
AttributeError: module ‘time‘ has no attribute ‘clock‘的解决方法
在python3.10中flask项目运行报错: AttributeError: module 'time' has no attribute 'clock'解决方案 主要原因是因为python3.10中不支持clock了, 需要替换成 time.perf_counter() cpmpat.py文件:
431 0