You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ImportError:无法从aiogram.utils导入executor的问题求助

解决aiogram中from aiogram.utils import executor导入报错的问题

你已经尝试重新安装pip,但执行代码时仍出现ImportError,你的代码示例如下:

from aiogram import Bot, Dispatcher, types
from aiogram.utils import executor

bot = Bot("")
dp = Dispatcher(bot)

@dp.message_handler(commands=['star'])
async def start(message: types.Message):
    await message.answer('Hello')

executor.start_polling(dp)

以下是针对性的解决方法:

  • 检查aiogram版本适配问题
    aiogram 3.x版本已经完全移除了executor模块,改用全新的启动方式。如果你的环境安装的是3.x版本,原导入语句必然报错:

    1. 查看当前aiogram版本:pip show aiogram
    2. 若为3.x,修改代码适配新版本写法:
      from aiogram import Bot, Dispatcher, types
      from aiogram.filters import CommandStart
      
      # 替换为你的机器人Token
      bot = Bot(token="your_bot_token_here")
      dp = Dispatcher()
      
      @dp.message(CommandStart())
      async def start(message: types.Message):
          await message.answer('Hello')
      
      if __name__ == "__main__":
          dp.run_polling(bot)
      
  • 回退到aiogram 2.x版本继续使用executor
    如果你想保留原代码逻辑,需要安装2.x的稳定版本:

    1. 卸载当前版本:pip uninstall aiogram -y
    2. 安装指定版本:pip install aiogram==2.25.1
    3. 安装完成后,原代码只需补全Bot的token即可正常运行
  • 确认Python环境与pip环境一致
    有时重新安装pip后,代码运行的Python环境和pip安装的环境不匹配,导致包未正确安装:

    1. 查看当前代码使用的Python路径:which python(Linux/macOS)或where python(Windows)
    2. 使用对应路径的pip安装依赖,例如:python -m pip install aiogram==2.25.1

内容的提问来源于stack exchange,提问作者user30067267

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 05:53:15