Jonathan Bowman
Posted on September 13, 2020
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.
Who are you? What do you want?
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.
I just want to get started
Choose Django. It includes all you will likely need, and has exceptional documentation.
Prefer a minimalist/modular approach? Choose Flask. Great community and documentation.
Better yet, take a quick glance at both Flask and Django, then make a choice or flip a coin. You will not go wrong with either.
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
I am learning Python, or learning to program
Oh, so many good resources. I will recommend two books that you can read for free online, or buy in print:
Automate the Boring Stuff with Python
The Hitchhiker's Guide to Python
These are not just about web development, but are great introductions to Python.
That said, there is no shame in diving right in, if you love the sink-or-swim approach. If so, choose Django or Flask and proceed with passion.
I heard of this thing called asyncio...
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
I want to build an API
Falcon is a great framework for getting a REST API framework built fast and running fast.
If you love the everything-and-the-kitchen-sink approach of Django, then the Django REST Framework is an add-on worth a look, and has a great community around it.
If you prefer the async approach mentioned earlier, then FastAPI is the right choice. There is a lot of love for that framework, built on Starlette.
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.
I just want to build a blog
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.
Lektor and Cactus also deserve a look.
If you are primarily writing documentation, MkDocs is easy, pretty, and popular.
We don't need no stinkin' frameworks (DIY, roll-your-own approach)
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 want to pursue the asynchronous route, you may appreciate my article on building your own simple ASGI app. To run an ASGI app, you will want to use one of the three excellent ASGI servers.
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.
Keeping up?
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.
Posted on September 13, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.