[英]Solution for - AttributeError: module 'time' has no attribute 'clock'
I have written code to create a ChatBot using HTML & python.我已经编写了代码来使用 HTML 和 python 创建一个聊天机器人。 I used python 3.8 but it fails when I tried to execute the form.
我使用了 python 3.8,但是当我尝试执行表单时它失败了。 I don't know how it fails because I used the latest version of python.
我不知道它是如何失败的,因为我使用了最新版本的 python。 I also tried some solutions published, but they also didn't work for me well.
我还尝试了一些已发布的解决方案,但它们也对我不起作用。 Help me with some solutions.
帮我解决一些问题。
Here's the Code:这是代码:
app.py应用程序.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索引.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>
And this is the error I got when I tried to execute the result in Python 3.8 version这是我尝试在 Python 3.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'
The error occurs because in python 2, there is time.clock()
, but in python 3, it has been replaced with time.perf_counter()
.发生错误是因为在 python 2 中有
time.clock()
,但在 python 3 中,它已被替换为time.perf_counter()
。
Just replace all the time.clock
to time.perf_counter
, and it should be fine.只需将所有
time.clock
替换为time.perf_counter
,就可以了。 For more info:https://www.webucator.com/blog/2015/08/python-clocks-explained/欲了解更多信息:https://www.webucator.com/blog/2015/08/python-clocks-explained/
From https://stackoverflow.com/a/62436862/11912082
来自https://stackoverflow.com/a/62436862/11912082
try this it worked for me:试试这个它对我有用:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.