无法从telegram导入Update?python-telegram-bot重装无效求助
解决ImportError: cannot import name 'Update' from 'telegram'问题
运行以下Telegram机器人代码时出现ImportError: cannot import name 'Update' from 'telegram'错误,已尝试执行pip uninstall python-telegram-bot、pip install python-telegram-bot --upgrade --force-reinstall操作仍未解决:
import requests from bs4 import BeautifulSoup from telegram import Update TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN' def start(update: Update, context: CallbackContext): context.bot.send_message(chat_id=update.effective_chat.id, text="Welcome to the product search bot! Please enter a product name to search for.") def search_product(update: Update, context: CallbackContext): product_name = update.message.text url = f'https://example.com/search?q={product_name}' response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') products = soup.find_all('div', class_='product') for product in products: name = product.find('h3', class_='product-name').text.strip() price = product.find('div', class_='product-price').text.strip() context.bot.send_message(chat_id=update.effective_chat.id, text=f'{name}\n{price}') def main(): updater = Updater(TOKEN) dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) dp.add_handler(MessageHandler(Filters.text & ~Filters.command, search_product)) updater.start_polling() updater.idle() if __name__ == '__main__': main()
问题原因
这个错误是因为python-telegram-bot v20及以上版本做了重大架构升级,原有的Update、Updater、CommandHandler等类的导入路径、API用法都发生了变化,旧代码的写法不再兼容新版本。
解决方案
方案一:适配新版本API(推荐)
修改代码以兼容python-telegram-bot v20+,修改后的完整代码如下:
import requests from bs4 import BeautifulSoup from telegram import Update from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN' async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): await context.bot.send_message(chat_id=update.effective_chat.id, text="Welcome to the product search bot! Please enter a product name to search for.") async def search_product(update: Update, context: ContextTypes.DEFAULT_TYPE): product_name = update.message.text url = f'https://example.com/search?q={product_name}' response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') products = soup.find_all('div', class_='product') for product in products: name = product.find('h3', class_='product-name').text.strip() price = product.find('div', class_='product-price').text.strip() await context.bot.send_message(chat_id=update.effective_chat.id, text=f'{name}\n{price}') def main(): application = ApplicationBuilder().token(TOKEN).build() start_handler = CommandHandler('start', start) search_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, search_product) application.add_handler(start_handler) application.add_handler(search_handler) application.run_polling() if __name__ == '__main__': main()
关键修改点:
- 导入路径调整:从
telegram.ext导入ApplicationBuilder、ContextTypes、filters,替代旧的Updater、CallbackContext、Filters - 处理函数改为异步函数:添加
async关键字,发送消息调用await context.bot.send_message() - 用
ApplicationBuilder替代Updater构建机器人应用
方案二:安装兼容旧代码的版本
如果不想修改代码,可安装最后一个支持旧API的稳定版本v13.7:
pip uninstall python-telegram-bot -y pip install python-telegram-bot==13.7
安装完成后,原代码无需修改即可运行,但无法使用新版本的功能和更新。
内容的提问来源于stack exchange,提问作者nikalaev
相关产品推荐
相关产品推荐

