Python Frameworks: 9 of the Best in 2021
Tivix
This isn't an article that needs a long introduction. You're looking for useful Python frameworks; our team of expert Python/Django developers are here to help.
The list below is partly based on popularity, but also on uniqueness, functionality, and general interest. You don't want to read another article listing relatively popular – but relatively samey – microframeworks, do you?
Have we missed anything out unfairly? Let us know – tweet us @Tivix with your favourite Python frameworks.
The gold standard: Django
The go-to of all Python frameworks, full-stack, open source Django allows programmers to create Python-based backends – fast.
What's there to like?
Whilst Django isn't the most popular of the Python frameworks on this list, there's plenty that developers will love here.
One of Django's key selling points is its speed. Its code-light approach, support for cookies, sessions and middleware, long list of database integrations and useful ORM with automatic migrations will help you get your app market-ready in no time (Bitbucket managed it in just a month using Django back in 08). None of the other Python frameworks comes close to how fast Django helps you develop.
Another major plus point is how easily scalable it is (due to its web nodes having no secure state, it's significantly easier to scale Django horizontally than many frameworks out there). Combined with its automatic application of security features like XSS and CRSF prevention, this makes it a tempting option for tech giants and early-stage startups alike.
Any drawbacks?
Not many – but the main issue devs have with Django is its 'convention over configuration' approach to coding. In its eagerness to make development as fast and as simple as possible, it imposes a very specific structure on the developer. This is fine a lot of the time, but can be an irritating hamper to creativity at others.
For the adventurous: Flask
Hot on Django's heels popularity-wise is Flask, an open source microframework best suited to those who are a) a dab-hand, coding-wise and b) don't need the lightning-fast time to market that Django offers.
If you love working in Python but find Django too restrictive, you're in luck.
As a microframework, Flask presents developers with a set of basic tools and gives them the freedom to explore from there. It offers routing, templating, session management and automatic debugging as well as being compatible with any ORM you choose (unlike Django, which throws a bit of a fit when you try to swap out its own ORM for another).
From there? It's up to you! If you're seriously into Python and like having the freedom to play around and experiment, this is where it gets exciting.
Whether this is a 'drawback' or not depends on your specific circumstances, but Flask won't make your structural decisions for you. If you're starting out from scratch, this means that you'll have to spend project time making decisions on things like folder structure and databases that full stack frameworks do automatically. Therefore, Flask is not a good choice for building a web app fast or MVP. Flask is a good framework for implementing microservice apps along with other microservice tools.
Whether this is a worthy trade off for the freedom flask offers depends on how much time you have, and whether you're a skilled enough Python developer to make the most of it.
Back to basics: Bottle
Pure, stripped back simplicity gets microframework Bottle a place on this list. You won't be using it to build your next million-download app, but it's a handy tool for small projects, prototyping and personal app development.
Bottle has no dependencies except the standard Python library, and your entire application lives in a single .py file. Simplicity at its finest, and absolutely ideal for creating small-scale apps or prototyping.
Bottle allows devs to work in the most minimalistic fashion. If this is an approach you like, you'll find a lot to enjoy here. It's also good as a 'starter' framework for those dipping into web development frameworks for the first time – you'll definitely feel the benefits of development using a framework, but it's not so code-light that it encourages you to be lazy.
The single file approach to code deployment isn't practical at scale – if you're working in a team of any number of people, you're going to have to deal with new requirements coming in and going out at a sometimes frustrating frequency. Having to do it all from a single file will only add to this frustration.
That said, Bottle has never pretended that it's a number one choice for enterprise app development. It does what it's designed to do very well, and we can't fault it as a personal coding and prototyping framework.
API specialist: Hug
The adorably-named Hug microframework uses a "write once, use anywhere" approach to creating web APIs. Whilst minimalist, there's plenty about it that will leave devs feeling warm and fuzzy.
Creating an API once and then using it wherever you want saves you so much time. Hug makes API development simple, offering devs multiple interfaces and inbuilt version management to manage their project. It also generates documentation automatically, which saves you having to write the docstrings and types annotations yourself – so you can do something better with your time.
Pre-processing requests can be shaky, especially when you need specified parameters. You can code workarounds that will fix this problem, but it's still pretty annoying.
For the RESTful: CherryPy
Another microframework, CherryPy has been around for the best part of a decade and offers some useful features not found in its rivals.
The same hands-off approach taken by Flask (its nearest rival) and other microframeworks like Bottle, with some well-thought-out extra features.
CherryPy distinguishes itself by offering a built-in multithreaded server, options to save your app either as a WSGI or HTTP app, and a super adaptable plugin system. On top of this, it can run on PyPy (if you're really looking to maximize performance), Jython and Android.
CherryPy is a great framework for building REST APIs. Flask is a good option here too, but reputationally CherryPy has the edge.
As well as the usual caveats you'd expect for microframeworks – they require more coding than a full stack, you're on your own more etc – CherryPy is considered relatively difficult to use by some Python developers. Its documentation also isn't quite as strong as other frameworks', but this does seem to have been improving over the past couple of years.
A different approach: CubicWeb
CubicWeb is an open source, full stack semantic framework that uses components called 'cubes' in place of views and models. These are joined together via a database, web server and configuration files to create an application.
CubicWeb is similar to Django in that it's all about reusable components – this saves a huge amount of time and means you don't have to code from scratch. There's also a range of useful features, including support for multiple databases, support for OWL, and some handy security workflows.
A major plus point is CubicWeb's RQL embedding. This makes it super easy to manipulate data repositories and browse through relations or attributes.
There's a lot of it. In some cases, this is an excellent thing. In others, the sheer number of functions CubicWeb offers can prove confusing, overbearing or downright unhelpful.
For performance: Tornado
Node.js' rise in popularity has given rise to a parallel a spate of asynchronous frameworks built for Python, designed to take advantage of the improved application performance, enhanced responsiveness asynchronous programming. Tornado is one of these frameworks.
Performance, performance, performance.
If your app needs to run time-consuming processes for a large user base, Tornado's asynchronous approach will really help you out. As well as templating tools, the framework offers support for real-time services, implementation of third-party authentication schemes, translation and localization, and does so whilst remaining lightning fast.
It doesn't require any external configuration and is relatively simple to get up and running, which is a nice little bonus too.
Not so much a drawback, but do you really need this much power? It's not the easiest of frameworks to use, and smaller apps could be served equally as well by something simpler. You may need to spend time getting everyone up to speed on asynchronous programming too, which could take a while.
It's also worth noting that Python is traditionally a single-threaded language. Most existing tools and libraries (excluding the asynchio library which Tornado runs off) were designed with this style of programming in mind, so you will run into frustrating compatibility issues at times. It'll be interesting to see if this changes with the rise of asynchronous programming's popularity though – watch this space.
AI focus: TensorFlow
One of Python's major advantages is that it's more or less the language used for AI and machine learning, which are becoming ever more present in our day-to-day lives. With this in mind, it would be rude not to include an AI development framework in this list at some point, and TensorFlow is the industry standard.
An open source platform originally developed by Google, TensorFlow is the most widely-used machine learning framework around right now. Feature-wise, there's support for regressions, classifications, neural network creation and algorithms, and it can be run on CPU, GPU, desktop or mobile – perfect for teams on the go.
Not necessarily a drawback – but TensorFlow is very code-heavy. Even if you know your stuff, there's a significant learning curve here. Once you get the hang of it there's plenty of scope for creativity, and the framework scales well, but don't expect to get going straight away.
TensorFlow's main structural weakness is that it uses clunky static graph computations, which slows things down quite a lot. More recent Python-based AI frameworks use dynamically updated graphs, so it'll be interesting to see whether TensorFlow can retain its popularity over the next few years.
One to watch: Masonite
Up-and-coming full-stack framework Masonite makes 'the Rails way' accessible to Python developers for an equally fast alternative to Django – but lacks the maturity for a major build.
If you're a fan of how Ruby On Rails does things, but don't have any Ruby developers to hand, this is the framework for you.
Essentially it's Laravel, but for Python – and if you've got any experience using Laravel you'll find it very easy to pick up indeed. It has the same emphasis on speed of development as Django, and its ambitious, rapidly expanding community certainly sees it as a potential rival to the Python behemoth a few years down the line.
Interesting features include an inbuilt CLI ('Craft') for scaffolding and running tasks, great support for unit testing and an IoC container for dependency injection.
If you hate 'the Rails way', you will hate Masonite – but then, different strokes for different folks. It's not for everyone.
The Masonite community is enthusiastic but young. You might struggle to find solutions to your issues when things go south, and it's probably not a mature enough framework to consider building anything major on right now. That said, it is fast and easy to use – for now, it's a good bet for creating quick prototypes (and potentially catching a glimpse of the next big Python framework in action while you're at it).
More articles byTivix
How to Build a Simple Native iOS application
Django Scaffolding
Getting Django Working with MSSQL / pyodbc