闲聊机器人:AttributeError: module 'time' has no attribute 'clock'

5

我正在尝试创建一个聊天机器人,但由于最新版本的chatterbot无法安装到我的电脑上,所以我使用 pip install chatterbot==1.0.4 安装了chatterbot,但出现了以下错误。

如何解决这个问题?

以下是代码:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

bot = ChatBot("My Bot")

convo = [
    'hello',
    'hi there',
    'what is your name?',
    'My name is BOT, I am created my a hooman ',
    'how are you',
    'i am doing great these days',
    'thank you',
    'in which city you live',
    'i live in kalyan',
    'in which languages do you speak',
    'i mostly talk in english'
    
]

trainer=ListTrainer(bot)

trainer.train(convo)

print("Talk to Bot")
while True:
    query=input()
    if query=='exit':
        break
    answer=bot.get_response
    print("bot : ", answer)

输出:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS E:\GIT VS CODE\Py> & D:/Python/python.exe "e:/GIT VS CODE/Py/chatbot.py"
Traceback (most recent call last):
  File "e:\GIT VS CODE\Py\chatbot.py", line 4, in <module>
    bot = ChatBot("My Bot")
  File "D:\Python\lib\site-packages\chatterbot\chatterbot.py", line 34, in __init__  
    self.storage = utils.initialize_class(storage_adapter, **kwargs)
  File "D:\Python\lib\site-packages\chatterbot\utils.py", line 54, in initialize_class
    return Class(*args, **kwargs)
  File "D:\Python\lib\site-packages\chatterbot\storage\sql_storage.py", line 22, in __init__
    from sqlalchemy import create_engine
  File "D:\Python\lib\site-packages\sqlalchemy\__init__.py", line 8, in <module>     
    from . import util as _util  # noqa
  File "D:\Python\lib\site-packages\sqlalchemy\util\__init__.py", line 14, in <module>
    from ._collections import coerce_generator_arg  # noqa
  File "D:\Python\lib\site-packages\sqlalchemy\util\_collections.py", line 16, in <module>
    from .compat import binary_types
  File "D:\Python\lib\site-packages\sqlalchemy\util\compat.py", line 264, in <module>    time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'
4个回答

9

我同意@Glycerine的答案,如果你不想降低Python版本,我有另一个解决方案。

打开文件\Lib\site-packages\sqlalchemy\util\compat.py 转到第264行,该行内容如下:


if win32 or jython:
    time_func = time.clock
    
else:
    time_func = time.time

将其更改为:


if win32 or jython:
    #time_func = time.clock
    pass
else:
    time_func = time.time

在这里,我们避免使用timer.clock方法和time_func对象,因为我发现这个对象只是无意义地被创建而且没有作用。它只是被创建出来然后不再改变。

我不知道为什么它存在,当它没有任何用途时。但这应该对你有用。

希望它能帮到你!


0
你正在运行哪个版本的Python?在Python 3.8+中,time.clock已被删除。
解决方案包括降级Python或更改源代码:
  • Python 3.8中模块'time'没有' clock '属性的AttributeError

    来自Python 3.8文档:

    函数time.clock()已被删除,在Python 3.3之后已被弃用:根据您的要求使用time.perf_counter()或time.process_time(),以获得定义良好的行为。(由Matthias Bussonnier在bpo-36895中提供。)

  • {{link2:解决- AttributeError:模块“时间”没有属性“时钟”}}


作为对您评论的回应:我认为Chatterbox开发人员最终会修复这个问题,但是降级到Python 3.7可以解决此问题:https://docs.python.org/3.7/library/time.html#time.clock

自版本3.3起已弃用,将在版本3.8中删除:此函数的行为取决于平台:根据您的要求使用perf_counter()或process_time()来具有良好的定义行为。


如果我安装Python 3.7,它会得到解决吗?我正在使用3.9.2。 - Tanish Batham

0

我不是专家,但你是否尝试在代码顶部使用'import time'。当我在我的Python代码中使用time.sleep()时,这对我有用。


0
我遇到了同样的错误,我尝试了这个代码:import time time.clock = time.time这个问题中,对我来说有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接