I’m trying to deploy a simple call and response bot to Heroku, but I keep getting the same error and I have no clue how to fix it. I have found that the program works from my personal computer, but not when I deploy it to Heroku. I feel that it has to do with my import statements:
import random
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler
from telegram.ext import Filters
from telegram.ext import messagequeue as mq
from telegram.utils.request import Request
import logging
import os
And I get these errors after pushing to heroku and running it:
2021-03-27T08:25:40.562359+00:00 heroku[web.1]: Starting process with command `python3 bog_bot.py`
2021-03-27T08:25:43.167956+00:00 heroku[web.1]: Process exited with status 1
2021-03-27T08:25:43.257029+00:00 heroku[web.1]: State changed from starting to crashed
2021-03-27T08:25:43.102105+00:00 app[web.1]: Traceback (most recent call last):
2021-03-27T08:25:43.102177+00:00 app[web.1]: File "/app/bog_bot.py", line 2, in <module>
2021-03-27T08:25:43.102489+00:00 app[web.1]: from telegram.ext import Updater
2021-03-27T08:25:43.102543+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/telegram/ext/__init__.py", line 21, in <module>
2021-03-27T08:25:43.102788+00:00 app[web.1]: from .basepersistence import BasePersistence
2021-03-27T08:25:43.102820+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/telegram/ext/basepersistence.py", line 25, in <module>
2021-03-27T08:25:43.103058+00:00 app[web.1]: from telegram import Bot
2021-03-27T08:25:43.103163+00:00 app[web.1]: ImportError: cannot import name 'Bot' from 'telegram' (/app/.heroku/python/lib/python3.9/site-packages/telegram/__init__.py)
I would really appreciate any help, as I’ve been searching for an answer to this little problem for a couple hours now.
4
Answers
So I figured out the problem was not that it was wrong in the requirements file, but in the pipfile. I removed a bunch of the requirements from there and it worked better. I still included the telegram library though, I think it may have been the pyTelegramBotAPI library that was causing me issues. I also restarted the project on a different app and made a couple changes to the bot's code. Now the import section looks like this:
Try remove
UPDATE
The
python-telegram-bot
modules uses the namespacetelegram
. So it mights cause error installing the two modules at together. To fix that you’ll need to uninstalltelegram
module. Since removing modules fromrequirements.txt
does not automatically removes the module, you will need to remove the module by yourself. See:Manually remove Python package on Heroku
Refrence:
https://github.com/python-telegram-bot/python-telegram-bot/issues/395
Make sure that both of your Pipfile and requirements.txt have only
python-telegram-bot
library.If there are other related libraries such as
teleram
orpython-telegram
, it will cause this kind of error.For example: ImportError: cannot import name ‘Animation’ from ‘telegram’ (/app/.heroku/python/lib/python3.9/site-packages/telegram/init.py)
This one resolved the issue:
https://github.com/mkdryden/telegram-stats-bot/issues/9
we only need the package
python-telegram-bot
nottelegram==0.0.1
You should try
pip uninstall telegram
,pip uninstall python-telegram-bot
and reinstall
python-telegram-bot
This will be your requirement.txt file
It should not contain :
telegram==0.0.1
Then ,
pip freeze > requirements.txt