There are many Python web frameworks. Most of them are quite good. You could probably write one yourself, as many have.
Yet you probably only need to know about one, maybe two.
You don't need the top 20 or even the top five, or the ones that are trending this year. You need one that works well for you, has good documentation and community support, and is kept reasonably up to date.
This article is for those exploring Python web development, who simply want to get started on their next project, trying out a Python web framework with which they were previously unfamiliar. It is intended to help you identify who you are, your desires, and pair you with a well-documented path forward.
This article is not:
- going to recommend the one perfect framework that beats all the rest. Such a creature does not exist.
- a comprehensive overview of all, or even most, Python web frameworks.
- necessarily going to include your favorite Python web framework, if you have already explored the landscape. Please know that you, and the framework you chose, are still awesome. Keep using the treasure you have found.
- a comparison of framework performance, or security, or trendiness.
- going to explain how Python is better than Node, Go, Rust, or .NET. It isn't better, and neither are they.
If you need any of the above, there are great resources out there. Because Python and web development are such a good match, searching the web for such topics is a rewarding endeavor.
Your next web development project in Python will be most satisfying after spending just a few moments in introspection. You might consider these questions:
- Do you prefer using tools that are all-inclusive, with everything you need, or do you prefer a modular approach, in which you start with a core, and then research and add features as needed? Put another way, do you want user authentication and RSS feeds included in your framework?
- Do you favor the satisfaction of the cutting edge, or the robust stability of tried-and-true?
- Are you new to Python or to programming in general?
- Do you know what an event loop is? In other words, do you come from Node or Go and want to explore similar patterns in Python?
These questions are just to get us started. The headings below ask similar and additional questions, followed by my recommendations.
Choose Django. It includes all you will likely need, and has exceptional documentation.
Prefer a minimalist/modular approach? Choose Flask. Great community and documentation.
Also worth mentioning:
- Bottle: even more minimalist (just one file). Fast and fun. One of my favorites.
- Pyramid: not micro or mega. Somewhere in between. Not nearly as popular as the others mentioned, but has good history and makes good choices
Oh, so many good resources. I will recommend two books that you can read for free online, or buy in print:
These are not just about web development, but are great introductions to Python.
Single threaded concurrency, event loops, async/await, oh my...
This is not a good starting place. But if you have some experience with Python, and/or with event loops (coming from Go or Node, for instance), or you just want to try the cutting edge, this is a good place to be.
If you just wanted the fastest Python web framework out there, this is not the right choice. Even though it is, arguably, where the fastest Python web frameworks are. I just don't think speed should be the only reason to explore Python asyncio.
Choose Starlette. Enough features, growing community and documentation, great technology.
Want something that has been around longer and earned its place over and over (and even includes an HTTP client, not just a server)? AIOHTTP is well designed, solid, and current.
Also worth mentioning:
- Django has async support
- FastAPI is built on Starlette and designed for building APIs
- Quart tries to be just like Flask, but with async support
- Sanic is very popular
- Tornado was async before async was cool
Falcon is a great framework for getting a REST API framework built fast and running fast.
Are you one of those GraphQL hipsters? Good for you. Thankfully, Graphene-Python is a library that can be paired with any of the above.
Any Python framework can be used to build a blog. But if you just want to start writing posts, with the reassurance that your blog engine has Python under the hood, then may I suggest that a static site generator may work well for you? Typically, you write in Markdown, generate the HTML, and post to your favorite web hosting, such as Netlify, Github Pages, or Gitlab Pages.
Use Pelican. It is solid and mature, and can import themes from Wordpress.
If you are primarily writing documentation, MkDocs is easy, pretty, and popular.
For some of us, re-inventing the wheel is way better than accepting someone else's design approach.
If this is you, then you will want to be familiar with at least one of the following interfaces:
You can build a basic WSGI app with nothing more than the Python standard library. There is even an example in the docs.
If you try to make your own, you will quickly discover why there are so many Python web frameworks. It is remarkably easy and rewarding.
You will likely be quite happy with any of the frameworks mentioned in this article. There is no need to worry that the one you are using is antiquated, insecure, or slow, as they are all kept up to date, and you really should be the judge of performance, not some theoretical benchmarks. In other words, its not slow unless it feels slow.
That said, if you like to keep your eyes on the landscape, you may appreciate my method: I periodically check Techempower's benchmarks. Expand the filters, select "Disable all" on Language, then select Python, and "Apply changes." To me, the performance is interesting, but shouldn't be overemphasized. What is interesting is the list, as it shows all major Python web frameworks, as well as a few up-and-coming. Regarding the new ones, take a look on Github to see how many maintainers they have, and how often the code is updated. They may be fast, but that doesn't mean they will be around two years from now.
Keeping up should only be a hobby on the side, if you find it enjoyable. You only need, at most, two frameworks: the one you are using now, and the one you are using next.