ImportError when using the Telegram Bot library after Python update

I’m encountering an issue with my Telegram bot developed using Django after attempting to upgrade the Python version. Initially, everything worked perfectly on Python 3.6.9, but due to Heroku not supporting it anymore, I had to update to 3.6.10.

Upon trying to run my bot, I’m facing the following error:

from .callbackcontext import CallbackContext File 
"/home/usr/bot-name/venv/lib/python3.6/site-packages/telegram/ext/callbackcontext.py",
line 21, in <module> from telegram import Update
ImportError: cannot import name 'Update' from 'telegram'

I also tested it with Python 3.7.6, which is still supported, but I received the same error when attempting to start the server:

python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
...
ImportError: cannot import name 'Update' from 'telegram' (/home/usr/bot-name/venv/lib/python3.7/site-packages/telegram/__init__.py)

Here are my current package dependencies:

cffi==1.13.2
cryptography==2.8
dj-database-url==0.5.0
Django==2.2.7
django-heroku==0.3.1
django-telegrambot==1.0.1
et-xmlfile==1.0.1
future==0.18.2
gunicorn==20.0.4
jdcal==1.4.1
mysqlclient==1.4.5
openpyxl==3.0.2
pipenv==2018.11.26
psycopg2==2.8.4
pycparser==2.19
python-telegram-bot==12.2.0
pytz==2019.3
six==1.13.0
sqlparse==0.3.0
telegram==0.0.1
tornado==6.0.3
virtualenv==16.7.9
virtualenv-clone==0.5.3
whitenoise==5.0.1

Can anyone help me identify the reason behind this import problem?

I encountered a similar situation while deploying on Heroku. The issue arises from conflicts between the python-telegram-bot and a placeholder telegram package that alters the import behavior. To resolve this problem, I removed both packages by executing pip uninstall telegram python-telegram-bot. After purging the pip cache with pip cache purge, I reinstalled only the python-telegram-bot. It’s also crucial to ensure that the requirements.txt only includes python-telegram-bot without the telegram entry, as it may have been added inadvertently through another dependency.

yeah, classic dependency conflict. the telegram==0.0.1 package is just a stub that’s overridding your actual python-telegram-bot module. remove that line from requirements.txt and do a fresh pip install - that’ll fix it. super common issue with heroku deployments.

Had this exact same problem deploying to Heroku last year. You’ve got both python-telegram-bot==12.2.0 and telegram==0.0.1 installed, and they’re conflicting. The telegram==0.0.1 package is basically empty but it’s blocking the real telegram module from python-telegram-bot. First run pip uninstall telegram, then reinstall python-telegram-bot. Delete that telegram==0.0.1 line from your requirements.txt too. Your imports should work fine after that. Fixed my ImportError instantly. Pretty sure django-telegrambot pulled in the wrong telegram dependency when you installed it.