ImportError: cannot import name filters from telegram.ext – Fix
In this article
The error ImportError: cannot import name 'filters' from 'telegram.ext' means your code is trying to import filters from the telegram.ext package, but that name isn't available there. In almost every case this is a version mismatch: filters was part of the older python-telegram-bot library (v13 and earlier). If you have v20 or newer installed, the library was reorganized and filters now lives in telegram.ext.filters — or you should use the new filters module directly from telegram.ext with a different import style. The fix is to either install the correct version or update your import statements to match the version you have.
What You'll Need
- Python 3.7 or newer installed on your system.
- Access to a terminal or command prompt.
- The
python-telegram-botlibrary installed (or the ability to install it). - Your bot script where the error occurs.
Step-by-Step
- Check your installed version. Open a terminal and run
pip show python-telegram-bot. Look at theVersion:line. If it starts with13.or lower, you are on the old API. If it starts with20.or higher, you are on the new API. This determines which fix you need. - If you are on v13 or lower and want to keep using
filtersas before: your import should befrom telegram.ext import filters(orfrom telegram.ext import Filtersin very old versions). If that still fails, your installation may be corrupted. Reinstall withpip install --upgrade --force-reinstall python-telegram-bot==13.15(or the latest 13.x version). - If you are on v20 or higher: the library was restructured. The
filtersmodule is now attelegram.ext.filters, but it is not exported directly fromtelegram.extin the same way. Change your import tofrom telegram.ext import filters— this works in v20+ becausefiltersis a submodule. If your code usesFilters(capital F), that was removed; usefilters(lowercase) instead. - Update your handler code for v20+. In v20, many constants moved. For example,
filters.TEXTstill exists, butfilters.Filtersdoes not. Check the official python-telegram-bot documentation for the version you have and adjust any filter references accordingly. - If you are mixing versions in the same environment, create a clean virtual environment:
python -m venv mybotenv, activate it, then install the version you need. This prevents conflicts with other packages. - Test the import in isolation. Run
python -c "from telegram.ext import filters; print('OK')". If that works, the problem is elsewhere in your script. If it fails, the version or installation is still wrong. - Restart your script after making changes. If you are using an IDE, restart the IDE or clear its cache so it picks up the new package.
Common Mistakes
- Using
Filterswith a capital F. In v20+, the class was renamed tofilters(lowercase). Code written for v13 often usesFilters, which no longer exists. - Installing the wrong package. The library is
python-telegram-bot, nottelegramortelegram-bot. Installing the wrong one leads to missing modules. - Not activating the virtual environment. If you install a package globally but run your script in a virtual environment (or vice versa), the import will fail. Always check
which pythonandpip showinside the same environment. - Assuming
filtersis a top-level module. It is a submodule oftelegram.ext, soimport filterswill not work. - Forgetting to update other imports. When migrating from v13 to v20, many other names changed (e.g.,
Updateris nowApplication). Fixing onlyfiltersmay reveal more errors.
Tips
- Pin your library version in
requirements.txt(e.g.,python-telegram-bot==20.7) to avoid surprise upgrades that break imports. - Read the migration guide. The python-telegram-bot GitHub wiki has a detailed v13 to v20 migration guide that lists every changed import and class.
- Use an IDE with import inspection (like VS Code with Pylance) to catch missing names before you run the script.
- Check for typos.
filtersis easy to misspell asfilterorfiltres. - If you are following an old tutorial, check its date. Tutorials written before v20 use the old API and will cause this error on new installs.
Troubleshooting
If the steps above did not resolve the error, work through these checks in order:
- Verify the package is actually installed. Run
pip list | grep telegram(Linux/macOS) orpip list | findstr telegram(Windows). You should seepython-telegram-botwith a version number. - Check for multiple Python installations. You may have installed the package for Python 3.9 but are running Python 3.11. Use
python --versionandpip --versionto confirm they match. - Uninstall and reinstall cleanly. Run
pip uninstall python-telegram-bot, thenpip install python-telegram-bot. This removes any partially installed or corrupted files. - Look for a local file named
telegram.py. If your script directory contains a file calledtelegram.py, Python may import that instead of the real package. Rename it. - Check your Python path. Run
python -c "import sys; print(sys.path)"and ensure the site-packages directory is included. If not, your environment is misconfigured. - Try a minimal script. Create a new file with just
from telegram.ext import filtersand run it. If it works, the issue is in your main script's other imports or code. - Consult the official documentation for the exact version you have. The API reference lists all available modules and classes.
Frequently Asked Questions
Why does from telegram.ext import filters fail?
It fails when the installed version of python-telegram-bot does not expose filters as a direct import from telegram.ext. In v20+, filters is a submodule and must be imported as from telegram.ext import filters — which actually works — but if you are on a version where it was removed or renamed, you need to adjust. Check your version first.
What version of python-telegram-bot uses Filters with a capital F?
Versions before 13.0 used Filters (capital F). From v13 onward, it became filters (lowercase). If you see Filters in your code, you are likely following an outdated tutorial.
Can I use filters with python-telegram-bot v20?
Yes, but the import style is from telegram.ext import filters. The module exists, but many individual filter constants and classes were renamed or moved. Refer to the v20 documentation for the correct names.
How do I downgrade to an older version to keep my code working?
Run pip install python-telegram-bot==13.15 (or another 13.x version). This reverts to the old API where filters is available as before. Be aware that older versions may not receive updates.
Why do I get this error even after installing the right version?
Likely causes: multiple Python environments, a local file shadowing the package, or a corrupted installation. Follow the troubleshooting steps above, especially checking sys.path and reinstalling cleanly.
Is telegram.ext.filters the same as telegram.filters?
No. telegram.filters does not exist in the official library. Always use telegram.ext.filters or import from telegram.ext directly.