Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

ImportError: cannot import name 'executor' from 'aiogram': how to fix it

ImportError: cannot import name 'executor' from 'aiogram' means code written for aiogram 2 is running on aiogram 3, which removed executor entirely. Rewrite the start for aiogram 3: Dispatcher() without the bot, handlers registered with @dp.message, and await dp.start_polling(bot) inside asyncio.run(main()). Pinning aiogram below 3 only postpones that rewrite.

By Livops team, the people who build and run LivopsUpdated

The bot stops at startup with this at the end of the traceback:

ImportError: cannot import name 'executor' from 'aiogram' (/home/me/bot/.venv/lib/python3.12/site-packages/aiogram/__init__.py)

The code was written for aiogram 2, and the aiogram that is installed is 3.x. This page explains what aiogram 3 removed, shows the same bot before and after the rewrite, lists the errors that come right after this one, and explains why pinning aiogram 2 is only a stopgap.

What does the error mean?

In aiogram 2 a bot was started by a helper called executor: executor.start_polling(dp)[6]. aiogram 3 removed it entirely; the Dispatcher now starts polling or handles webhooks itself[1]. The package no longer exports executor at all[2], so from aiogram import executor fails, and so does from aiogram.utils import executor.

The same release changed two more things that every aiogram 2 bot uses[1]:

  • The Dispatcher no longer takes the bot. Dispatcher(bot) becomes Dispatcher(), and the bot is passed only when polling starts or a webhook update is handled. That is what lets one dispatcher serve several bots.
  • The _handler suffix is gone from every decorator: @dp.message_handler() becomes @dp.message(), and @dp.callback_query_handler() becomes @dp.callback_query().

The executor is usually the first line Python trips on, because it sits in the first import. Fix only that and the next error is waiting a few lines further down.

Which aiogram is installed?

Run this with the same Python the bot runs with:

pip show aiogram
python -c "import aiogram; print(aiogram.__version__)"

Version 3.0.0 and later have no executor. aiogram 3.0.0 was released in September 2023[8], so a requirements.txt that says only aiogram, or aiogram>=2.20, now installs aiogram 3 on every fresh install, while an old laptop or server still has 2.x. That is why the same code works in one place and fails in another.

The fix: rewrite the start for aiogram 3

A typical aiogram 2 bot, close to the project’s own 2.x example[6]. This is the code that fails on aiogram 3:

import os

from aiogram import Bot, Dispatcher, executor, types

bot = Bot(token=os.environ["BOT_TOKEN"])
dp = Dispatcher(bot)


@dp.message_handler(commands=["start"])
async def start(message: types.Message):
    await message.answer("Hi!")


@dp.message_handler()
async def echo(message: types.Message):
    await message.answer(message.text)


if __name__ == "__main__":
    executor.start_polling(dp, skip_updates=True)

The same bot for aiogram 3, following the 3.x example[5]:

import asyncio
import logging
import os

from aiogram import Bot, Dispatcher, F
from aiogram.filters import CommandStart
from aiogram.types import Message

dp = Dispatcher()


@dp.message(CommandStart())
async def start(message: Message) -> None:
    await message.answer("Hi!")


@dp.message(F.text)
async def echo(message: Message) -> None:
    await message.answer(message.text)


async def main() -> None:
    bot = Bot(token=os.environ["BOT_TOKEN"])
    # aiogram 3 has no skip_updates=True: drop the queued updates yourself
    await bot.delete_webhook(drop_pending_updates=True)
    await dp.start_polling(bot)


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

What changed, line by line:

  • No executor. asyncio.run(main()) starts the event loop once, and await dp.start_polling(bot) runs inside it[5]. Do not call asyncio.run again anywhere inside the bot; why is in This event loop is already running.
  • Dispatcher() without the bot, which goes to start_polling(bot) instead[1].
  • @dp.message(...) instead of @dp.message_handler(...), with filters as objects: CommandStart() or Command("start") instead of commands=["start"][1].
  • F.text on the echo handler. In aiogram 2 a handler without arguments implicitly received only text messages outside any FSM state; in aiogram 3 it receives every content type in every state[1]. Without F.text, the first sticker reaches message.text as None.
  • skip_updates=True is gone. To skip what queued up while the bot was down, call delete_webhook(drop_pending_updates=True) before polling[1].

Handlers in several files: Router

The Dispatcher is itself the root Router[4]. When handlers live in their own modules, give each module a Router and include it into the Dispatcher:

# handlers.py
from aiogram import F, Router
from aiogram.types import Message

router = Router()


@router.message(F.text == "ping")
async def ping(message: Message) -> None:
    await message.answer("pong")
# bot.py
import asyncio
import os

from aiogram import Bot, Dispatcher

from handlers import router


async def main() -> None:
    bot = Bot(token=os.environ["BOT_TOKEN"])
    dp = Dispatcher()
    dp.include_router(router)
    await dp.start_polling(bot)


if __name__ == "__main__":
    asyncio.run(main())

Old line, new line

aiogram 2 aiogram 3
executor.start_polling(dp) await dp.start_polling(bot) inside asyncio.run(main())
Dispatcher(bot) Dispatcher()
@dp.message_handler(...), @dp.callback_query_handler(...) @dp.message(...), @dp.callback_query(...)
commands=["start"] Command("start") from aiogram.filters
content_types=ContentType.PHOTO F.photo
text="hello" F.text == "hello"
message.get_args() command: CommandObject in the handler, then command.args
Bot(token, parse_mode="HTML") Bot(token, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
aiogram.contrib.fsm_storage.memory.MemoryStorage aiogram.fsm.storage.memory.MemoryStorage

All of these come from aiogram’s migration guide[1], which lists many more: keyboards, callback data, middlewares, exceptions, i18n.

The errors that come next

The same migration produces a family of errors. Each is the same cause, a line written for aiogram 2, and is fixed by the table above. Run on aiogram 3 today, they read:

Error The aiogram 2 line
TypeError: Dispatcher.__init__() takes 1 positional argument but 2 were given dp = Dispatcher(bot): the initializer now takes keyword arguments only[3]
AttributeError: 'Dispatcher' object has no attribute 'message_handler' @dp.message_handler(...)
ModuleNotFoundError: No module named 'aiogram.contrib' from aiogram.contrib.fsm_storage.memory import MemoryStorage
ModuleNotFoundError: No module named 'aiogram.dispatcher.filters' from aiogram.dispatcher.filters import Text (the Text filter has no 3.x equivalent; use F.text)[1]
ImportError: cannot import name 'FSMContext' from 'aiogram.dispatcher' now from aiogram.fsm.context import FSMContext
ImportError: cannot import name 'ParseMode' from 'aiogram.types' now from aiogram.enums import ParseMode
TypeError: Passing `parse_mode`, `disable_web_page_preview` or `protect_content` to Bot initializer is not supported anymore Bot(token, parse_mode="HTML"): removed in 3.7.0, the message names the replacement[7]

On Python 3.9 and older the first TypeError reads __init__() without the class name; the cause is the same.

Some changes raise nothing at all, and aiogram’s migration guide marks them as the dangerous group[1]: a bare handler that now receives stickers and photos, parse_mode=None in a call, which now turns formatting off instead of using the bot’s default, and await bot.me, which is now a method, await bot.me(). After the rewrite, test the bot with a sticker, a photo and every command before you deploy it.

Why pinning aiogram 2 is a dead end

The quick way out is to install the version the code was written for:

aiogram==2.25.2

It works as a stopgap, and it costs more every month:

  • No more releases. 2.25.2, from October 2023, is the last 2.x release; everything since is 3.x[8].
  • Old dependencies. aiogram 2.25.2 requires aiohttp 3.8[9]. aiohttp 3.8.6, the last 3.8 release, ships ready-built wheels only up to Python 3.11[10], so on Python 3.12 or newer pip has to compile it from source. When that fails, see Failed building wheel.
  • The rewrite is still ahead. The migration only grows with the code you add on top of aiogram 2.

If you pin, pin Python too (3.11 or older), and treat it as time to do the rewrite, not instead of it. Going the other way, current aiogram 3 releases declare Python 3.11 or newer, below 3.16[8].

On Livops

What Livops does with an aiogram bot, and what it does not:

  • It installs what requirements.txt names. An unpinned aiogram line gets the current 3.x release on every deploy, the same as on any fresh machine. Write the major version you mean: aiogram>=3,<4 once the code is rewritten.

  • The error stops the deploy as start_failed. The build passes, the bot fails as it starts, and the traceback is on the project’s Logs page. A bot’s previous version is stopped before the new one starts, because two copies polling Telegram would conflict, so the bot is down until a deploy succeeds. Run the rewritten bot locally first.

  • You choose the Python. Livops runs Python 3.10 to 3.14 and reads the version from a .python-version file at the project root. For current aiogram 3, use 3.11 or newer; for the aiogram 2 stopgap, 3.11 is the newest Python with ready aiohttp 3.8 wheels.

  • The start command. Livops finds bot.py, main.py, app.py or run.py by itself. To name it, write .venv/bin/python bot.py in Settings → Start, or a Procfile:

    worker: .venv/bin/python bot.py
  • The token is a variable. Put it under Settings → Variables as BOT_TOKEN, which the code above reads.

  • Starting fresh is also an option: the aiogram 3 bot + Redis template creates an aiogram 3 bot together with a Redis database.

A bot costs 19 credits a month (19,000 so’m). A VPS is the better fit when you run many bots on one machine, or a bot needs root for system packages of its own; on Livops a push is the deploy and restarts are the platform’s job, but there is no root. Hosting details are in aiogram bot hosting, and startup failures in The app did not start.

Questions

Why did this start without any change to my code?

Your requirements.txt names aiogram without a version, or with only a lower bound, so a fresh install picked the current 3.x release. The code was written for aiogram 2. Check the installed version with pip show aiogram.

Can I just import executor from somewhere else?

No. aiogram 3 removed the executor completely, not only moved it. from aiogram.utils import executor fails the same way. The Dispatcher starts polling itself with await dp.start_polling(bot).

Is pinning aiogram 2 a real fix?

It is a stopgap. The last 2.x release, 2.25.2, came out in October 2023 and needs aiohttp 3.8, which has no ready-built wheels for Python 3.12 or newer. The rewrite to aiogram 3 still has to happen.

What replaces dp.message_handler?

dp.message, or router.message on a Router you include into the Dispatcher. aiogram 3 dropped the _handler suffix from every decorator, so callback_query_handler becomes callback_query as well.

Why does my text handler now crash on stickers and photos?

A bare v2 handler only received text messages outside any FSM state. A bare v3 handler receives every content type in every state. Add the filter explicitly: dp.message(F.text).

Which Python does aiogram 3 need?

The current 3.x release on PyPI declares Python 3.11 or newer, below 3.16. On an older Python pip installs an older 3.x release or none at all.

Does Livops rewrite aiogram 2 code for me?

No. Livops installs what requirements.txt names and starts your bot. When the bot fails at startup, the deploy stops as start_failed and the traceback is on the Logs page; the rewrite is yours.

Sources

  1. aiogram docs source — Migration FAQ (2.x -> 3.x) (github.com). Checked .
  2. aiogram source — aiogram/__init__.py (github.com). Checked .
  3. aiogram source — aiogram/dispatcher/dispatcher.py (github.com). Checked .
  4. aiogram docs source — Dispatcher (github.com). Checked .
  5. aiogram — examples/echo_bot.py (3.x) (github.com). Checked .
  6. aiogram — examples/echo_bot.py (2.x) (github.com). Checked .
  7. aiogram source — aiogram/client/bot.py (github.com). Checked .
  8. PyPI — aiogram, release history (pypi.org). Checked .
  9. PyPI — aiogram 2.25.2 (pypi.org). Checked .
  10. PyPI — aiohttp 3.8.6, files (pypi.org). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your bot

Got a question? — @livops_uz