1

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.

Jonathan Bear
  • 11
  • 1
  • 1
  • 3
  • I'm not a expert, but did you have the file `requirements.txt`?https://devcenter.heroku.com/articles/python-pip You need to list modules that isn't in python's standard library you want to use in it. – Lab Mar 27 '21 at 08:35
  • Yes, I can include it here: telegram==0.0.1 appdirs==1.4.4 APScheduler==3.6.3 certifi==2020.12.5 chardet==4.0.0 distlib==0.3.1 filelock==3.0.12 idna==2.10 importlib-metadata==1.5.0 more-itertools==4.2.0 pipenv==11.9.0 pyTelegramBotAPI==3.7.6 python-telegram-bot==13.4.1 pytz==2021.1 requests==2.25.1 six==1.14.0 telebot==0.0.4 tornado==6.1 tzlocal==2.1 urllib3==1.26.4 virtualenv==20.4.3 virtualenv-clone==0.3.0 zipp==1.0.0 – Jonathan Bear Mar 27 '21 at 08:43

4 Answers4

4

Try remove

telegram==0.0.1 

UPDATE

The python-telegram-bot modules uses the namespace telegram. So it mights cause error installing the two modules at together. To fix that you'll need to uninstall telegram module. Since removing modules from requirements.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

Lab
  • 196
  • 3
  • 18
  • @JonathanBear From posts I read related to this question, the solutions are all about python-telegram-bot is not installed (or installed correctly). Or maybe is it the module version doesn't match? – Lab Mar 27 '21 at 08:50
  • My requirements file came directly from my pipenv freeze, and I'm not sure how to install the file correctly if it's not correctly installed here – Jonathan Bear Mar 27 '21 at 08:52
  • Can you do `heroku run pip list`? See also https://github.com/fabston/TradingView-Webhook-Bot/issues/5 – Lab Mar 27 '21 at 08:55
  • It is there and version 13.4.1 – Jonathan Bear Mar 27 '21 at 08:59
  • So, I'm not sure if you remove a module from requirements.txt uninstalls the module from heroku or not. Is telegram==0.0.1 still there in heroku? – Lab Mar 27 '21 at 09:02
  • According to https://github.com/python-telegram-bot/python-telegram-bot/issues/395 the module also uses the telegram namespace, this might cause problem. – Lab Mar 27 '21 at 09:02
  • Yes, the telegram module is still there – Jonathan Bear Mar 27 '21 at 09:04
  • @JonathanBear This might help you remove the telegram module: https://stackoverflow.com/questions/15046378/manually-remove-python-package-on-heroku Is the telegram module required in your program, though? – Lab Mar 27 '21 at 09:05
  • Im not sure if it is or isnt. Ive been working on this for a while and I forget what I actually still need or dont need – Jonathan Bear Mar 27 '21 at 09:13
  • @JonathanBear You need to remove the module from heroku to solve this namespace problem. The link above is the way to remove module from heroku. – Lab Mar 27 '21 at 09:41
1

This one resolved the issue:

https://github.com/mkdryden/telegram-stats-bot/issues/9

we only need the package python-telegram-bot not telegram==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

APScheduler==3.6.3
cachetools==4.2.2
certifi==2022.6.15
python-telegram-bot==13.13
pytz==2022.1
pytz-deprecation-shim==0.1.0.post0
six==1.16.0
tornado==6.2
tzdata==2022.1
tzlocal==4.2

It should not contain : telegram==0.0.1 Then , pip freeze > requirements.txt

Falgun
  • 11
  • 2
0

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:

import random
from telegram import Update
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
import telegram
from telegram.utils.request import Request
import logging
import os
Jonathan Bear
  • 11
  • 1
  • 1
  • 3
0

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 or python-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)

Nora
  • 161
  • 1
  • 6