【问题标题】:Time module: Couldn't find a version that satisfies the requirement时间模块:找不到满足要求的版本
【发布时间】:2021-05-29 21:10:51
【问题描述】:

我已尝试运行此代码:

from time import clock

def f2():
    t1 = clock()
    res = ' ' * 10**6
    print('f2:', clock()-t1)

但得到了 Traceback:

from time import clock
ImportError: cannot import name 'clock' from 'time' (unknown location)

Python 在标准库中没有看到时间模块?

我尝试通过 pip 手动安装这个模块(是的,我知道它应该已经安装了。但是我还能做什么呢?)。我收到以下错误响应:

ERROR: Could not find a version that satisfies the requirement time
ERROR: No matching distribution found for time

尝试通过 PyCharm 安装模块也失败了 - 它只是运行 pip 并得到相同的错误。

【问题讨论】:

    标签: pip pycharm python-module


    【解决方案1】:

    我找到了答案。

    模块 time 中的方法 clock() 在 3.8 中已弃用,请参阅 issue 36895

    所以我用time.time()

    import time
    
    def f2():
        t1 = time.time()
        res = ' ' * 10**8
        t2 = time.time()
        print('f2:', t2 - t1)
    

    奇怪,但是在谷歌上搜索这个问题时,我注意到很多人在 2019-2021 年(在clock( 之后在 Python 3.8 中弃用)都有这个错误,但没有人写出如何解决它。 所以我的回答可能真的很有帮助。

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 1970-01-01
      • 2023-03-20
      • 2021-12-25
      • 2018-10-13
      • 2019-07-18
      • 2020-04-15
      • 2015-11-24
      • 2013-08-16
      相关资源
      最近更新 更多