Python is both a fun toy and a frightening flamethrower. The same goes for what you can do with Python.
Python is loved by hobbyists, scientists, and architects alike.
It’s damn easy to get started with, has higher-order abstractions and metaprogramming capabilities to build large and complex systems, and has truckloads of libraries for doing pretty much anything. Sure, there are limitations when it comes to concurrency and strong typing, but you can work around them.
In this article, we’ll cast a look at some of the best Python frameworks when it comes to building web applications large and small.
The Django framework has withstood the test of time the go-to web framework for the Python community. If you assaulted a Python developer in their sleep and forced them to build a web application at gunpoint, they’d automatically reach for Django the way a Rubyist will reach for Rails.
And there’s a good reason for that. Django is, as the tagline says, “a web framework for perfectionists with deadlines.” It’s what is called a “batteries included” framework (much like how Python is a batteries-included language), which provides all common functionality out of the box.
With these features baked in, Django massively cuts down on development time:
- A handy and pleasant ORM, with migrations created and applied automatically by the framework.
- Scaffolding for automatic generation of admin panel based on your models.
- Support for cookies, sessions, middleware, templates, etc.
- Security features like XSS prevention, CRSF prevention, etc., are applied automatically.
- Works with practically all databases out there (it’s easy to find adapters where official support doesn’t exist)
- First-class support for Geographical data and spatial queries though GeoDjango
And much, much more. Suffice it is to say Django is a full-blown, friendly web framework.
Is Django for you?
Django makes excellent sense for all use cases, whether rapid prototyping or planning an enterprise application. The only rough edge you’ll come across is the framework’s structure.
Since Django bends over backward to make development fast and easy for you, it imposes its structure (a concept called “convention over configuration”) on the developer, which you may not agree with.
For instance, if you want to replace the Django ORM with something else (say, SQL Alchemy), be prepared for surprises.
Interested in becoming full-stack developer with Django and Python? – Check out this fantastic online course.
While Django takes up almost all the mindshare in Python web development, Flask is a strong contender.
As opposed to Django, Flask is a “micro-framework,” which means it focuses on getting a few bare minimum things right and leaves the rest to you. This “the rest is up to you” can be a source of frustration or delight, depending on what your goals are.
For those that know what they’re doing and want to lovingly craft their web applications by choosing components of their choice, Flask is a godsend.
Flask offers the following features:
- Routing, templating, session management, and other useful features.
- Full support for unit-testing
- A minimal, pluggable architecture
- First-class REST support
- Support for Blueprints, Flask’s unique take on architecture for tiny web applications
- Choose your packages for ORM, migrations, etc.
- Flexible application structure — put your files where they make the most sense to you
- Static file serving
- WGSI compliant
Is Flask for you?
As already said, Flask is a minimal web framework, with everything broken up into individual components that you can swap out. If you’re in a hurry to build a prototype, you’ll spend a lot of time making trivial decisions on the database, folder structure, routing, etc., that can prove counter-productive. Flask works best when you’re on to a stable, serious project of medium- to large-scale, especially REST APIs.
If you thought Flask’s approach wasn’t minimal enough and that you’d still like more control, say hello to Bottle.
Bottle strips out even more, to the point where the only dependency is the Python standard library. This means no pip install this or pip install that, though you’d most likely need to before long. Here’s why Bottle stands out for some people:
- Single-file deployment. Yes, your entire application lives in a single “.py” file.
- No external dependencies. If you have the right Python version installed, you’re good to go.
- Supplies its templating engine, which can be swapped out with Jinja2, Mako, or Cheetah.
- Support for forms, headers, cookies, and file uploads.
- Built-in web server, which can be easily replaced.
Is Bottle for you?
If you’re making a really small app (say, less than 500 lines of code) with no special requirements, Bottle might make a lot of sense to you. It’s a complete no-nonsense approach to creating web apps, but in practice, you’ll find you’re more hindered than helped by Bottle.
The reason is that the real world is always changing and before you know it. New requirements will be dropped on your head. At that point, putting everything in a single file would become a chore.
Also, if you think Bottle and Flask are almost alike, you’re right. Proposals of merging the two date back to 2012, and even Armin, the creator of Flask, agrees with that. However, Marcel, the creator of Bottle, maintains a strict ideological distinction because of the single-file approach and insists that the two remain separate.
Zope is complicated to explain, but I’ll attempt. Yes, it’s a web framework that can be used to build applications large or small, but that’s not all. Zope is more of a platform that consists of tools (web frameworks included) created under the Zope philosophy and maintained by the Zope Corporation.
Zope has several interesting components and features suitable for enterprise application development:
- A component registering and discovery architecture to configure a large app.
- ZODB — (the only) object database for Python for storing objects natively.
- Full-fledged framework and standards for Content Management Systems
- A set of web application frameworks — the canonical one is still called Zope, although several new frameworks (like Grok) have been built on top of it.
- Strong standards for software development, release, and maintenance.
Is Zope for you?
If you’re after a highly structured environment for building really large apps, Zope is good. That said, you’ll run into your fair share of issues as well. While Zope continues to evolve, the community is really small, to the extent that many Python developers haven’t even heard of it.
Finding tutorials and extensive documentation is hard, so be prepared to do a lot of digging around (though the community is really helpful!). Also, the Python developers you come across may not want to learn Zope and “dilute” their skill-set.
TurboGears is a highly flexible framework that has a genuinely composable architecture. It’s a framework that scales as per your needs, and you can use it to build a single-file application or a multi-tenancy behemoth.
TurboGears has some elegant features, some of which are either not present in popular frameworks (like Django) or are hard to build:
- First-class support for multiple databases
- Multi-database transactions
- Highly modular — start with a single file and scale out as much as you need
- A powerful ORM (SQLAlchemy, which is more mature and capable than Django’s ORM)
- Pluggable architecture based on the WSGI specification
- Built-in support for database sharding
- A function-driven interface as opposed to deep, rigid object-oriented hierarchies.
Is TurboGears for you?
If you want to develop happily and want a tested, mature, and robust framework away from the media noise of “awesome, next-gen” and all that, TurboGears is a great fit. It’s highly respected in the community and has complete, extensive documentation.
Sure, TurboGears isn’t opinionated, which means initial setup and configuration time can be more, but it’s the ideal framework for enterprise application development.
Web2py started as a hobby project and was released in 2007. Its goals are to make web development easy and accessible to everyone.
As a result, Web2py takes the zero-dependency approach to the extreme — it has no requirements, nothing to install, and includes a full-featured Web-based editor for development, database management, as well as deployment.
You can almost think of it as Android Studio, which is more of a complete environment than just a framework. Some nice features that Web2py has, are:
- Virtually no learning curve.
- Minimal core (only 12 objects), which can even be memorized!
- Pure-Python templating
- Protection against XSS, CSRF, and other attacks
- A pleasant and consistent API
Is Web2py for you?
Web2py is a stable and fun framework, but it’s hard to recommend it against other options like Django, Flask, etc. There are hardly any jobs, and the unit testing story is not great. That said, you might enjoy the code API and the overall experience the framework offers, especially if you’re building REST APIs.
CherryPy is another microframework that aims to provide basic framework functionality and quickly get out of the way.
While it’s comparable to other microframeworks like Flask, CherryPy boasts of some distinction:
- It contains a built-in multi-threaded server (something that remains on the wishlist of Flask)
- The (single) web server can host multiple applications!
- Serve your application as a WSGI app (to interface with other WSGI apps) or a plain HTTP server (which performs better)
- First-class support for profiling and unit-testing
- Runs on PyPy (for the true performance junkies), Jython, and even Android
CherryPy does all this, and then the usual you’d expect from a web framework.
Is CherryPy for you?
If you’re building RESTful services mostly, CherryPy is a much more serious contender than Flask. It’s a decade-old framework that has matured nicely and is suitable for small and large applications alike.
The rise of Node and its asynchronous programming model left many communities feeling behind, Python’s included. In response, a flurry of async web frameworks have appeared on the radar, Sanic being one of them.
Sanic is heavily inspired by Flask, to the extent that it borrowed the route decorators, Blueprints, and other fundamentals hook line and sinker. And they’re not ashamed to admit it. What Sanic brings to the table, if you’re a Flask fan, is true non-blocking I/O to meet the performance levels of a Node application. In other words, Sanic is Flask with
When compared to CherryPy, Sanic has an incredible performance advantage (just think of how it would fare against Flask!). Check out the following results tested by DataWeave:
As you can see, once the concurrency numbers start exceeding 50 per second, CherryPy practically chokes and throws up a high failure rate.
Is Sanic for you?
While the performance characteristics of Sanic blow everything else out of the water, it may not be the best choice for your next project. The main reason is the lack of asynchronous libraries.
The bulk of existing Python tools and libraries were written for the single-threaded CPython version, with no forethought for high concurrency or asynchronous operations. If, for example, your favorite ORM does not support asynchronous operations, the whole point of using Sanic gets defeated.
Because of these maturity and availability reasons, we won’t examine any more async frameworks in Python.
I came across this framework a while ago and thought it was a step in the right direction. Since then, version 2.0 has been released, and I feel like the time has finally come to give Masonite some love.
Simply put, Masonite is the Python version of Laravel (a famous PHP framework, in case you didn’t know). Why does that matter? It matters because Laravel was built on the principles of Ruby on Rails, and together these two frameworks allow non-Ruby devs to experience the “Rails Way” of doing things.
Laravel (and to an extent, Rails) developers will feel right at home and would be up and running in literally no time. When I tried Masonite (and I did submit an issue or two, including a bug!), I was able to build REST APIs with exactly zero thinking because my Laravel muscle memory was doing everything.
As a batteries-included, full-stack framework, Masonite brings several interesting things to the table:
- Active-record style ORM
- Database migrations (which, unlike Django, need to be created by the developer)
- A powerful IoC Container for dependency injection
- Own CLI (called “craft”) for scaffolding and running tasks
- First-class support for unit testing
The biggest “rival” for Masonite is Django, as the community is doing its best to market the framework as easy, delightful, and the next big thing. Whether it will surpass Django is something time will tell (if you ask me, it does have a decent shot), but for a discussion comparing the two, see here.
Is Masonite for you?
Masonite is still a baby when compared to Django, so there’s no way it can be recommended over Django. That said, if you’re into the Rails way (or the Laravel way) of doing things, you’d appreciate what Masonite has to offer. It’s ideal for rapidly building prototypes that need everything pre-configured and easy to switch.
Falcon is a Python web framework designed for large-scale microservices.
The library has a complete system with templates, complimentary packages, and add-ons that you can use in your project. The Falcon is an awesome starting point to get all the resources you need.
The project also has a strong community where users discuss the design and development of this framework.
Falcon offers the following features;
- Debuggable: You can easily debug a Falcon app as it tells you which inputs lead to which outputs. Luckily, all the unhandled exceptions on Falcon are never encapsulated or masked.
- Flexible: Falcon gives you control over implementation details, allowing you to customize the app as you see fit. The minimalist approach makes it easier to debug and tune your app as you see fit.
- Fast: Falcon has been proven to turn around requests faster than other frameworks. This framework works perfectly with PyPy.
- ASGI and WSGI support: You can use Falcon irrespective of whether you are a fan of Asynchronous Server Gateway Interface (ASGI) or Web Server Gateway Interface (WSGI). WGSI is synchronous, meaning it handles requests one after the other (sequentially). ASGI is asynchronous, where requests are processed simultaneously.
Is Falcon right for you?
Falcon is a good choice when you are building microservices and mission-critical REST APIs. The fact that this framework is designed to be fast makes it an excellent choice for apps processing multiple transactions at-a-go.
Pyramid is presented as “The Start Small, Finish Big, Stay Finished Framework”. If you are a learner, you will find Pyramid easy to get started.
The framework allows you to choose the approach to use for security, templating, database, and so much more. You can even start with a scaffold.
Pyramid offers the following features;
- Scalability: The fact that you can start small does not mean your application will remain like that forever. Pyramid has an add-on, extension, and configuration system to help you scale your app.
- Authentication and authorization: You can protect your app from unauthorized access through its built-in authorization and authentication features.
- Extensible: Pyramid is designed as a full package for the entire app’s lifecycle. The platform is committed to API stability to ensure you always have APIs you can trust.
- Highly customizable: You can customize or override the core code without forking. This approach allows you to create apps that suit your needs.
Is Pyramid for you?
You should use Pyramid when building content-driven and large-scale web applications. The modular and flexible architecture makes it easy to scale your apps. Pyramid offers easy integration with databases and has built-in support for flexible routing, making it a perfect choice for content sites such as news platforms and blogs.
On top of being a Python framework, Tornado is an asynchronous networking library. Unlike most Python frameworks that are based on WSGI, Tornado runs with only one thread per process.
You can still get support for WSGI through tornado.wsgi. However, most developments are based on the
tornado.web, Tornado’s own interface.
Tornado offers the following features;
- Non-blocking network I/O: This library is built on top of the asyncio library. This enables it to handle thousands of connections at a go without much overhead.
- Security and authentication: Tornado has security features such as cross-site request forgery protection, HTTPS, and cookies. The same library has inbuilt authentication support.
- Scalability: You can increase capacity or add more servers as the need arises while using Tornado.
- WebSocket support: There is real-time communication between the server and the client through the built-in WebSocket implementation.
Is Tornado for you?
Tornado will be a perfect option if you want to build high-performance web services. This Python framework is highly scalable and its non-blocking I/O feature makes it a perfect fit if you are building a service that will handle a big volume of simultaneous connections.
Tornado is also suitable for applications handling a lot of I/O operations like processing large files or fetching data from external APIs.
CubicWeb is a semantic web application framework that allows developers to build applications following object-oriented design principles.
The framework has a set of libraries and tools that developers can use to build powerful applications.
CubicWeb is packed with the following features;
- Reusable components: You can save development time by reusing components across your app.
- Uses a query language: CubicWeb uses RQL, a query language in the background.
- Compatible with multiple databases: Take advantage of CubicWeb’s compatibility with Mercurial, LDAP directories, and SQL databases.
- Customizable: CubicWeb is flexible and customizable, allowing you to control how your app works.
Is CubicWeb right for you?
CubicWeb is a good choice if you want to build apps that require flexible querying and data modeling capabilities. You can thus build social networking, knowledge management, and scientific data management applications using CubicWeb.
There’s no shortage of Python frameworks out there, large and small. While you can pick up pretty much anything for a small project, an enterprise application has demands that not many of these frameworks can fulfill. If you ask me, for enterprise development, Django (to an extent), Zope, and TurboGears are what come to mind. And even among those, I’m inclined towards TurboGears.
That said, any architect worth his salt can pick up a microframework and roll out their architecture. And this is pretty much what happens in practice, which explains the success of Flask and similar ideas.
If you are a newbie, then this online course would be helpful for learning Python.
Next, explore some of the specialized Python frameworks to build APIs.