AttributeError的解决方案:模块“time”没有属性“clock”

2024-10-02 10:31:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了使用HTML&;创建聊天机器人的代码;python我使用了Python3.8,但在尝试执行表单时失败了。我不知道它是如何失败的,因为我使用了最新版本的python。我也尝试了一些发表的解决方案,但它们对我也不起作用。帮我解决一些问题

代码如下:

app.py


    #import files
    from flask import Flask, render_template, request
    from chatterbot import ChatBot
    from chatterbot.trainers import ChatterBotCorpusTrainer
    from chatterbot.trainers import ListTrainer

    app = Flask(__name__)

    bot = ChatBot("Python-BOT")

    trainer = ListTrainer(bot)
    trainer.train(['what is your name?', 'My name is Python-BOT'])
    trainer.train(['who are you?', 'I am a BOT'])

    trainer = ChatterBotCorpusTrainer(bot)
    trainer.train("chatterbot.corpus.english")


    @app.route("/")
    def index():
        return render_template('index.html')


    @app.route("/get")
    def get_bot_response():
        userText = request.args.get('msg')
        return str(bot.get_response(userText))


    if __name__ == "__main__":
        app.run()


index.html


    <!DOCTYPE html>
    <html>
      <head>
        <title>Python-BOT</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <style>
          * {
            box-sizing: border-box;
          }
          body,
          html {
            height: 100%;
            margin: 0;
            font-family: Arial, Helvetica, sans-serif;
          }
          #chatbox {
            margin-left: auto;
            margin-right: auto;
            width: 40%;
            margin-top: 60px;
          }
          #userInput {
            margin-left: auto;
            margin-right: auto;
            width: 40%;
            margin-top: 60px;
          }
          #textInput {
            width: 90%;
            border: none;
            border-bottom: 3px solid black;
            font-family: monospace;
            font-size: 17px;
          }
          .userText {
            color: white;
            font-family: monospace;
            font-size: 17px;
            text-align: right;
            line-height: 30px;
          }
          .userText span {
            background-color: #808080;
            padding: 10px;
            border-radius: 2px;
          }
          .botText {
            color: white;
            font-family: monospace;
            font-size: 17px;
            text-align: left;
            line-height: 30px;
          }
          .botText span {
            background-color: #4169e1;
            padding: 10px;
            border-radius: 2px;
          }
          #tidbit {
            position: absolute;
            bottom: 0;
            right: 0;
            width: 300px;
          }
          .boxed {
            margin-left: auto;
            margin-right: auto;
            width: 78%;
            margin-top: 60px;
            border: 1px solid green;
          }
        </style>
      </head>
      <body>
        <div>
          <h1 align="center">
            <b>Welcome to Python-BOT</b>
          </h1>
        </div>
      </body>
    </html>


这就是我在Python3.8版本中尝试执行结果时遇到的错误


    F:\Projects\python_projects\ChatBot-sample-3>"F:/Installed Software/python/phython 3.8/python.exe" f:/Projects/python_projects/ChatBot-sample-3/app.py   
    Traceback (most recent call last):
      File "f:/Projects/python_projects/ChatBot-sample-3/app.py", line 9, in <module>
        bot = ChatBot("Python-BOT")
      File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\chatterbot\chatterbot.py", line 34, in __init__
        self.storage = utils.initialize_class(storage_adapter, **kwargs)
      File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\chatterbot\utils.py", line 54, in initialize_class
        return Class(*args, **kwargs)
      File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\chatterbot\storage\sql_storage.py", line 22, in __init__
        from sqlalchemy import create_engine
      File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\__init__.py", line 8, in <module>
        from . import util as _util  # noqa
      File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\__init__.py", line 14, in <module>
        from ._collections import coerce_generator_arg  # noqa
      File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\_collections.py", line 16, in <module>
        from .compat import binary_types
      File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\compat.py", line 264, in <module>
        time_func = time.clock
    AttributeError: module 'time' has no attribute 'clock'


Tags: infrompymarginimportapplinewidth
2条回答

发生此错误的原因是,在python 2中存在time.clock(),但在python 3中,它已被替换为time.perf_counter()

只要将所有的time.clock替换为time.perf_counter,就可以了。有关详细信息:https://www.webucator.com/blog/2015/08/python-clocks-explained/

From https://stackoverflow.com/a/62436862/11912082

试试这个,它对我有用:

if win32 or jython:
    try: # Python 3.4+
        preferred_clock = time.perf_counter
    except AttributeError: # Earlier than Python 3.
        preferred_clock = time.clock
else:
    time_func = time.time

相关问题 更多 >

    热门问题