5

I am trying to create a telegram bot. The code i am trying to execute is :

from telegram import ParseMode

But it is throwing up this error:

ImportError: cannot import name 'ParseMode' from 'telegram'
(C:\ProgramData\Anaconda3\lib\site-packages\telegram\__init__.py)

Could you please advise how to fix this error?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60

2 Answers2

11

as per Version 20 you need to use:

from telegram.constants import ParseMode
S. Nick
  • 12,879
  • 8
  • 25
  • 33
Yeabsira Ki3ret
  • 111
  • 1
  • 3
  • Hiya, when you have code in your answer, it's helpful for readers to put it inbetween sets of three backticks ```. Stack Overflow will then comment the snippet as code! – Tim J Oct 27 '22 at 06:21
0

you have to import with this way:

from telegram.ext import ParseMode

if problem not solved:

install the package like this:

pip install python_telegram_bot

or

pip install "python_telegram_bot==12.4.2"

  • Thanks for your response. I am still getting an error: `ImportError: cannot import name 'ParseMode' from 'telegram.ext'` – Slartibartfast Sep 12 '21 at 23:38
  • did you try to install last version of `telegram`? or uninstall the package and install that with this commands? `pip install python_telegram_bot` – Hamidreza Khorammfar Sep 12 '21 at 23:42
  • I installed it and it imported in well – Hamidreza Khorammfar Sep 12 '21 at 23:43
  • i used `pip install telegram` and i was able to import `import telegram.ext` just not parsemode. Should i uninstall it ? – Slartibartfast Sep 12 '21 at 23:57
  • yes uninstall the `telegram` and install `python_telegram_bot` – Hamidreza Khorammfar Sep 13 '21 at 00:03
  • i uninstalled it and install `python_telegram_bot` but i am still getting the same message. Just to be sure also restarted the kernal – Slartibartfast Sep 13 '21 at 00:11
  • 1
    This answer is misleading at least. [`ParseMode` ](https://python-telegram-bot.readthedocs.io/en/stable/telegram.parsemode.html) is clearly not contained in the `telegram.ext` package. Also `python-telegram-bot` is by now on version 13.7. Older versions don't get bug fixes and v13 introduced breaking changes. Finally, the project is named `python-telegram-bot`, not `python_telegram_bot`. While `pip` apparently converts `_` to `-` due to naming conventions, this may not be the case with other package managers or future pip versions. Disclaimer: I'm currently the maintainer of PTB. – CallMeStag Sep 13 '21 at 05:52