Chatterbot : AttributeError: module 'time' has no attribute 'clock'

Chatterbot : AttributeError: module 'time' has no attribute 'clock'

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

我该如何解决这个问题?

代码如下:

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'

你 运行 python 是什么版本? time.clock 已从 py 3.8+

中删除

解决方案包括降级 python 或更改源代码:

  • AttributeError: module 'time' has no attribute 'clock' in Python 3.8

    From the Python 3.8 doc:

    The function time.clock() has been removed, after having been deprecated since Python 3.3: use time.perf_counter() or time.process_time() instead, depending on your requirements, to have well-defined behavior. (Contributed by Matthias Bussonnier in bpo-36895.)

  • Solution for - AttributeError: module 'time' has no attribute 'clock'


回应您的评论:我假设话匣子开发者最终会解决这个问题,但是是的,降级到 Python 3.7 会解决这个问题:https://docs.python.org/3.7/library/time.html#time.clock

Deprecated since version 3.3, will be removed in version 3.8: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.

我不是专家,但您是否在代码顶部尝试过 'import time'。当我在我的 pythoncode

中使用 time.sleep() 时为我工作

我同意@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 对象,因为我看到这个对象只是白白创建,什么也不做。它刚刚被创建并且保持不变。

我不知道为什么连这个都没有用。但这应该适合你。

希望对您有所帮助!