Python loads a telegram module that is not python-telegram-bot
Fix it fast
Most common cause, 1 of 3The unrelated PyPI package called telegram is installed
Uninstall the look-alike, then reinstall python-telegram-bot with --force-reinstall. pip treats python-telegram-bot as already present, so a plain install would skip it.
python -m pip uninstall telegram
python -m pip install --force-reinstall python-telegram-botShort answer
ImportError: cannot import name 'Update' from 'telegram' means Python found a module called telegram that is not python-telegram-bot: the unrelated PyPI package named telegram, a telegram.py in your folder, or another environment's copy. Print telegram.__file__, run pip uninstall telegram, then pip install --force-reinstall python-telegram-bot. Applies to python-telegram-bot 22.8 and earlier.
Which cause do you have?
Ask the failing interpreter which telegram it imports and whether the look-alike package is installed. A path inside your project points to cause 2. A site-packages path together with a hit for the package named telegram points to cause 1. If pip finds python-telegram-bot somewhere other than the environment of the printed interpreter, go to cause 3.
python -c "import sys, telegram; print(sys.executable); print(telegram.__file__)"
python -m pip show telegram python-telegram-botThen find the line you see in this list and open its fix:
- 1The unrelated PyPI package called telegram is installed
pip show lists Name: telegram with Version: 0.0.1, or the error ends in (unknown location) after you removed that package - 2A telegram.py file or telegram folder in your project hides the library
The message ends with a path inside your project, such as (/home/me/bot/telegram.py), or mentions a partially initialized module 'telegram' - 3python-telegram-bot sits in one environment while another Python runs the bot
pip show python-telegram-bot reports a Location that is missing from the sys.path of the interpreter that fails, or python -m pip show prints WARNING: Package(s) not found
What does the error look like?
ImportError: cannot import name 'Update' from 'telegram'ImportError: cannot import name 'Animation' from 'telegram' (unknown location)
Cause 1 of 3
The unrelated PyPI package called telegram is installed
You seepip show lists Name: telegram with Version: 0.0.1, or the error ends in (unknown location) after you removed that package
Check
PyPI carries a separate project named telegram, last released as 0.0.1 in 2015, that has nothing to do with python-telegram-bot. Both use the import name telegram, so the wrong one can answer the import, and it has no Update class. The python-telegram-bot FAQ lists installing it by mistake as the first reason for this error.
python -m pip show telegramFix
Uninstall the look-alike, then reinstall python-telegram-bot with --force-reinstall. pip treats python-telegram-bot as already present, so a plain install would skip it. That matters because removing the other package can also delete files at paths both projects used, which leaves (unknown location) in the message.
python -m pip uninstall telegram
python -m pip install --force-reinstall python-telegram-botVerify
python -m telegram prints the python-telegram-bot version, and the import runs.
python -c "from telegram import Update; import telegram; print(telegram.__version__)"Confirmed by github.com
Cause 2 of 3
A telegram.py file or telegram folder in your project hides the library
You seeThe message ends with a path inside your project, such as (/home/me/bot/telegram.py), or mentions a partially initialized module 'telegram'
Check
Before it looks in site-packages, Python checks the folder of the script being run, so your own telegram.py or telegram/ directory wins over the installed library. The FAQ gives this as the second usual reason and calls it a namespace clash.
ls -d telegram.py telegram/ 2>/dev/nullFix
Give the file or folder another name, for example my_bot.py, and remove the __pycache__ folder next to it so no compiled copy of the old name is left behind.
mv telegram.py my_bot.py
rm -rf __pycache__Verify
telegram.__file__ now points into site-packages/telegram/ and the bot starts.
python -c "import telegram; print(telegram.__file__)"Confirmed by github.com
Cause 3 of 3
python-telegram-bot sits in one environment while another Python runs the bot
You seepip show python-telegram-bot reports a Location that is missing from the sys.path of the interpreter that fails, or python -m pip show prints WARNING: Package(s) not found
Check
The library can be installed correctly and still be invisible: pip wrote it to the site-packages of one Python, while your editor, service file or shell starts another one that may carry an old or broken telegram module. The FAQ's third reason is this mismatch between pip show's Location and sys.path.
python -c "import sys; print(sys.executable); print(sys.path)"
python -m pip show python-telegram-botFix
Run pip through the same interpreter that starts the bot, so both point at one environment. The FAQ also mentions adding the Location folder to PYTHONPATH, but installing into the right interpreter keeps the setup simpler to repeat.
/path/to/the/bots/python -m pip install python-telegram-bot
/path/to/the/bots/python bot.pyVerify
pip show reports Version: 22.8 with a Location inside the environment that sys.executable belongs to, and the import succeeds.
python -m telegramConfirmed by github.com
How do I stop it coming back?
- List python-telegram-bot~=22.8 in requirements.txt and never the bare name telegram, which is the other project.
- Name your bot file bot.py or main.py, never after a library the code imports.
- Give each bot its own venv and point the editor, systemd unit or Dockerfile at that venv's python.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.