I am trying to create a chatbot, but since latest version of chatterbot was not getting installed on my pc so I installed chatterbot by using pip install chatterbot==1.0.4 and the following error is showing up.
How do I resolve this?
Below is the code:
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)
output :
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'
I agree with #Glycerine's answer, well, if you don't want to downgrade your python version, I have another solution for you.
Open the file <Python-folder>\Lib\site-packages\sqlalchemy\util\compat.py
Go to line 264 which states:
if win32 or jython:
time_func = time.clock
else:
time_func = time.time
Change it to:
if win32 or jython:
#time_func = time.clock
pass
else:
time_func = time.time
Simply here we are avoiding the use of timer.clock method and the time_func object because I saw that this object is just created for nothing and does nothing. It is just created and left untouched.
I don't know why even this exists when it is of no use. But this should work for you.
Hope it helped!
What version of python are you running? time.clock has been removed for py 3.8+
Solutions include downgrading python or altering the source it seems:
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'
In Response to your comment: I'm assuming the chatterbox devs will fix this eventually but yes, downgrading to Python 3.7 will fix this: 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.
Im not an expert, but have you tried 'import time' in the top of your code. Worked for me when I was using time.sleep() in my pythoncode
i am recently learning python so this is my code
import firebase
firebase = firebase.FirebaseApplication("https://cleandzapp.firebaseio.com/")
data ={
'Name': 'nana',
'Email':'ghjjkk',
}
result = firebase.post('cleandzapp/student',data)
print(result)
and this is the error i got
C:\Users\Marc\PycharmProjects\pythonProject\venv\Scripts\python.exe "C:\Data\nana\studY\CS\2CS\S2\arduino\mini projet\CleanDzApp\firebase_test.py"
Traceback (most recent call last):
File "C:\Data\nana\studY\CS\2CS\S2\arduino\mini projet\CleanDzApp\firebase_test.py", line 1, in <module>
import firebase
File "C:\Users\Marc\PycharmProjects\pythonProject\venv\lib\site-packages\firebase\__init__.py", line 19, in <module>
from sseclient import SSEClient
ModuleNotFoundError: No module named 'sseclient'
Process finished with exit code 1
the initial problem has that solution ___>the async became a key word in the latest versions of pip so you should change the file async in this path C:\Users\username\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\firebase into something else like nasync , also change the ---- from sync import process pool---- in init.py and firebase.py (in the same path) into to ---- from nsync import process pool---- , that should solve the problem
I'm have a script 'zigbot.py' which was working, but I'm restructuring to dockerize and now I'm questioning my ability to code at all. What I'm trying to do is have a telegram-bot container, and a 'web' container (FLASK), and a nginx container.
Next to zigbot.py I have a folder - 'bot', which has many of my scripts and functions in. Upon trying from bot.somescript import a_function I am getting error after error.
Project structure
zigbot
bot
bot
__init__.py
conversations.py
ct.py
Docerfile
funcs.py
pricedata.py
requirements.txt
util.py
zigbot.py
nginx
somestuff
web
app
migrations
templates
__init__.py
config.py
forms.py
functions.py
models.py
routes.py
signals.py
__init__.py
Dockerfile
requirements.txt
zigweb.py
When running the code below, I get a whole range of strange errors - the one listed below shows ImportError - it can't find 'onboard' in funcs, but its definitely there. Before the restructure, this was working. If I comment out 'onboard' it errors on every function in the list.
from bot.conversations import key_conversation
from bot.crypto_functions import satoshi_to_btc
from bot.funcs import onboard, main_menu, new_message1, new_keyboard, onboarded_keyboard, \
onboarded_message, blank_signal_message, send_signal_format, get_or_create_user, signal_detected_keyboard, log_me
Traceback (most recent call last):
File "zigbot.py", line 15, in <module>
from bot.funcs import onboard, main_menu, new_message1, new_keyboard, onboarded_keyboard, \
File "ZigBot\bot\bot\funcs.py", line 2, in <module>
from zigbot.web.app import db, app
File "ZigBot\bot\zigbot.py", line 15, in <module>
from bot.funcs import onboard, main_menu, new_message1, new_keyboard, onboarded_keyboard, \
ImportError: cannot import name 'onboard'
So I tried adding a .bot.conversation for (relative?) import, but this produces an even weirder error. I've included the code, the error, then the function I'm trying to import below:
from .bot.conversations import key_conversation
from .bot.crypto_functions import satoshi_to_btc
from .bot.funcs import onboard, main_menu, new_message1, new_keyboard, onboarded_keyboard, \
onboarded_message, blank_signal_message, send_signal_format, get_or_create_user, signal_detected_keyboard, log_me
from ..web.app import db
from ..web.app.models import User, Signal
Traceback (most recent call last):
File "zigbot.py", line 13, in <module>
from .bot.conversations import key_conversation
ModuleNotFoundError: No module named '__main__.bot'; '__main__' is not a package
conversations.py
# Initialise conversationHandler states
def key_conversation(cancel):
# Define keyboards
keyboard = [
[InlineKeyboardButton('Previous', callback_data='onboard:key'),
InlineKeyboardButton('Next', callback_data='onboard:alldone')]]
conversation_keyboard = InlineKeyboardMarkup(keyboard)
Finally, I think I'm getting closer to the problem, I go back to the way I think it's supposed to be imported. From bot.funcs import x,y,y... I look in funcs.py and this is how its importing some other code from my Flask web app, but it doesn't like it.
funcs.py
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from web.app import db, app
from web.app.models import User
Traceback (most recent call last):
File "zigbot.py", line 15, in <module>
from bot.funcs import onboard, main_menu, new_message1, new_keyboard, onboarded_keyboard, \
File "ZigBot\bot\bot\funcs.py", line 2, in <module>
from web.app import db, app
ModuleNotFoundError: No module named 'web'
So I change the import to go up a level since web is two levels above bot - which contains the script I'm importing to. So zigbot>bot>bot>funcs.py trying to import from zigbot>web>app>
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from ..web.app import db, app
from ..web.app.models import User
import telegram
Traceback (most recent call last):
File "zigbot.py", line 15, in <module>
from bot.funcs import onboard, main_menu, new_message1, new_keyboard, onboarded_keyboard, \
File "\ZigBot\bot\bot\funcs.py", line 2, in <module>
from ..web.app import db, app
ValueError: attempted relative import beyond top-level package
This obviously doesn't work either. Given my structure of having bot and web how do I get this to work? I even tried pulling the scripts out of the second 'bot' folder but I get the same issues. Finally, my weirdest error which might provide a clue to what I'm doing wrong, if I change the import on funcs.py to go one level up, rather than two, I get an even stranger traceback.
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from .web.app import db, app
from .web.app.models import User
import telegram
Traceback (most recent call last):
File "zigbot.py", line 15, in <module>
from bot.funcs import onboard, main_menu, new_message1, new_keyboard, onboarded_keyboard, \
File "C:\Users\phill\PycharmProjects\ZigBot\bot\bot\funcs.py", line 2, in <module>
from .web.app import db, app
ModuleNotFoundError: No module named 'bot.web'
For clarity I removed some 'C:\Users\phill' from the tracebacks before realising it was pointless to remove that.
I think there's a fundamental flaw in your split if you have library dependencies across the boundary. Assuming that there are no imports of bot-modules in your web container, I would suggest breaking off the shared code into a module named weblib or something (alongside the bot-, nginx- and web-folders).
You still should not try to import across would-be container boundaries however, so just make sure weblib is in your path or installed in your virtual environment or however you want to handle it. Basically, bot and web should run in separate environments and should have weblib as a dependency.
Someone wiser than me might be able to explain it better.
For relative imports to work all the packages and subpackages must be in the sys.path - to achieve this you should run from the top-level directory as in:
C:\Users\phill\zigbot>python -m bot.zigbot
This will make your current working dir (zigbot) available to sys.path and the subpackages will be resolved correctly
I've been learning how to use Python and Slack for my class project. I'm particularly working on slack threads so I can direct message my entire class.
import os
from slackclient import SlackClient
slack_token = os.environ["xoxb-restofapitoken"]
sc = SlackClient(slack_token)
sc.api_call(
"chat.postMessage",
channel="#general",
text="Hello from Python! :tada:",
thread_ts="1476746830.000003",
reply_broadcast=True
)
When I run the code, the error below shows.
Traceback (most recent call last):
File "chat1.py", line 4, in <module>
slack_token = os.environ["xoxb-restofapitoken"]
File "/home/ubuntu/starterbot/lib/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'xoxb-restofapitoken'
What am I doing wrong?
I've met similar error before.
There is nothing wrong with your code.
However, kindly follow Slack's guideline when you are using test tokens , "pass tokens in as environment variables".
Change the code to:
import os
from slackclient import SlackClient
slack_token = os.environ["SLACK_BOT_TOKEN"]
sc = SlackClient(slack_token)
sc.api_call(
"chat.postMessage",
channel="#general",
text="Hello from Python! :tada:",
thread_ts="1476746830.000003",
reply_broadcast=True
)
Run it with:
SLACK_BOT_TOKEN="xoxb-restofapitoken" python myapp.py
and you should be good to go.
I am using the following code to sign in to twitter:
import simplejson
import twitter
import oauth
import oauthtwitter
from oauthtwitter import OAuthApi
class OauthRequest():
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
AUTHORIZATION_URL = 'http://twitter.com/oauth/authorize'
REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'
def GetRequest(self):
vOauthApi = OAuthApi(self.CONSUMER_KEY, self.CONSUMER_SECRET)
self.mOauthRequestToken = vOauthApi.getRequestToken(self.REQUEST_TOKEN_URL)
self.mOauthRequestUrl = vOauthApi.getAuthorizationURL(self.mOauthRequestToken)
However, I receive the following error message:
Traceback (most recent call last):
File "C:\Python33\twitter.py", line 4, in <module>
import oauthtwitter
File "C:\Python33\lib\site-packages\oauthtwitter.py", line 17, in <module>
from twitter import Api, User
ImportError: cannot import name Api
I have installed PIP and downloaded the 'oauth', 'oauthtwitter' and 'twitter' modules using it. I am running Windows Vista and Python 3.3. As far as I can tell in my 'Lib' folder the files are there for oauthtwitter.
I'm noy sure exactly what is going wrong here?
Thanks