ModuleNotFoundError with telegram.parsemode when launching my Telegram bot

I’m facing an import error every time I try to run my bot script. The error indicates that Python can’t find the telegram.parsemode module, although I have installed the telegram library.

Here’s the error traceback I’m encountering:

(venv) C:\Users\MyUser\Desktop\chat_bot>python my_telegram_bot.py
Traceback (most recent call last):
  File "C:\Users\MyUser\Desktop\chat_bot\my_telegram_bot.py", line 2, in <module>
    from telegram.parsemode import parse_mode
ModuleNotFoundError: No module named 'telegram.parsemode'

I’ve tried reinstalling the python-telegram-bot package several times and experimenting with the different version combinations, but the problem continues to occur.

Here’s how my bot code begins:

# THE SECTION WHERE THE ERROR OCCURS

from telegram import Update, ParseMode
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import random
import time

# BOT CONFIGURATION

API_TOKEN = '987654321:EXAMPLE_TOKEN_STRING_HERE'

# Game state variables
lotto_numbers = [str(num) for num in range(1, 75)]  # Numbers 1-75
active_game = False
drawn_numbers = []
user_cards = {}
winner_count = {}
administrator_ids = [123456789]  # Admin user IDs

# Generate random numbers for players
def create_player_card() -> dict:
    """Creates random number cards for each participant"""
    participant_cards = {}
    for user_id in user_cards:
        participant_cards[user_id] = random.sample(lotto_numbers, 4*4)  # 16 numbers per card
    return participant_cards

# Welcome command
async def welcome(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """Initial greeting message"""
    await update.message.reply_text(
        "Welcome to Number Game Bot! Only group admins can start new games using /begin.\n"
        "Use /info to learn the rules.",
        parse_mode=ParseMode.MARKDOWN
    )

# Help command
async def info(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """Display help information"""
    await update.message.reply_text(
        "To play the number game, use /begin to start a new round.\n"
        "Numbers will be called out and you mark the ones on your card!",
        parse_mode=ParseMode.MARKDOWN
    )

# Start game function (admin only)
async def begin_game(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """Initiates the number game"""
    global active_game, drawn_numbers, user_cards
    current_user = update.message.from_user.id

Looking at your code, you actually have the correct import statement already - from telegram import Update, ParseMode is right. The issue might be that you have a conflicting line somewhere else in your code that’s causing the error. Check if you have from telegram.parsemode import parse_mode anywhere in your file, because that’s what the traceback shows on line 2. Also worth checking if you have multiple versions of the telegram library installed or if there are any cached bytecode files causing conflicts. Try deleting any __pycache__ folders in your project directory and run the script again. Sometimes Python gets confused with cached imports.

sounds like a common mix-up! yeah, just import ParseMode straight from telegram like you mentioned. that should fix the error. had the same problem once, it’s easy to overlook.

It appears you’re having an import confusion with your bot script. Based on the error traceback, you should ensure that you’re importing ParseMode from the telegram module, not telegram.parsemode. Your correct import line should look like from telegram import ParseMode. This typically resolves the ModuleNotFoundError. Additionally, confirm that your installed version of the python-telegram-bot library is the latest, as older versions may cause compatibility issues. If the problem persists, try reinstalling the package.