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))


相关文章
|
5月前
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‘
129 0
|
1月前
|
编解码 算法 Python
ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’
ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’
20 0
|
4月前
|
Go
go time.After() time.Ticker
go time.After() time.Ticker
23 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
602 0
解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matching
|
9月前
|
Python
【已解决!】ImportError: cannot import name ‘ProjectModelSerializer‘ from partially initialized module ‘pr
【已解决!】ImportError: cannot import name ‘ProjectModelSerializer‘ from partially initialized module ‘pr
|
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文件:
308 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'
|
Python
AttributeError module ‘time‘ has no attribute ‘clock‘ 解决方法
在 Pycharm + Python3.8 环境中,调用 time.clock() 报错:AttributeError: module ‘time’ has no attribute ‘clock’
827 0
AttributeError module ‘time‘ has no attribute ‘clock‘ 解决方法
|
Python
import statsmodels.api as sm 时 ImportError: DLL load failed while importing _arpack: 找不到指定的程序。
import statsmodels.api as sm 时 ImportError: DLL load failed while importing _arpack:
913 0
import statsmodels.api as sm 时 ImportError: DLL load failed while importing _arpack: 找不到指定的程序。

热门文章

最新文章