Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Python loads a telegram module that is not python-telegram-bot

Your error

ImportError: cannot import name 'Update' from 'telegram'1 more line it can show

Fix it fast

Most common cause, 1 of 3

The 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.

Fix
python -m pip uninstall telegram
python -m pip install --force-reinstall python-telegram-bot
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to python-telegram-bot 22.8, Python 3.10 to 3.15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
python -c "import sys, telegram; print(sys.executable); print(telegram.__file__)"
python -m pip show telegram python-telegram-bot

Then find the line you see in this list and open its fix:

  1. 1The unrelated PyPI package called telegram is installedpip show lists Name: telegram with Version: 0.0.1, or the error ends in (unknown location) after you removed that package
  2. 2A telegram.py file or telegram folder in your project hides the libraryThe message ends with a path inside your project, such as (/home/me/bot/telegram.py), or mentions a partially initialized module 'telegram'
  3. 3python-telegram-bot sits in one environment while another Python runs the botpip 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.

Check
python -m pip show telegram

Fix

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.

Fix
python -m pip uninstall telegram
python -m pip install --force-reinstall python-telegram-bot

Verify

python -m telegram prints the python-telegram-bot version, and the import runs.

Verify
python -c "from telegram import Update; import telegram; print(telegram.__version__)"

Confirmed by github.com

Did this fix it?

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.

Check
ls -d telegram.py telegram/ 2>/dev/null

Fix

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.

Fix
mv telegram.py my_bot.py
rm -rf __pycache__

Verify

telegram.__file__ now points into site-packages/telegram/ and the bot starts.

Verify
python -c "import telegram; print(telegram.__file__)"

Confirmed by github.com

Did this fix it?

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.

Check
python -c "import sys; print(sys.executable); print(sys.path)"
python -m pip show python-telegram-bot

Fix

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.

Fix
/path/to/the/bots/python -m pip install python-telegram-bot
/path/to/the/bots/python bot.py

Verify

pip show reports Version: 22.8 with a Location inside the environment that sys.executable belongs to, and the import succeeds.

Verify
python -m telegram

Confirmed by github.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.