Created in the early 1990s by Guido van Rossum, Python has grown in popularity over the years. In 2016, Python is the 4th most popular language after Java, C and C++. Python is a general purpose programming language, and it can be used in a variety of fields. To quote Kenneth Love from Treehouse —
When I need to build a web app, I reach for Python. When I need to automate some small task on my system, I reach for Python. When I want to find the most common colors in an image, I reach for Python.
Python is a popular choice for writing scripts for testing and monitoring. Python has also been used for game development, and its ability to be integrated with other languages makes it very valuable in the process. Such is the popularity of Python that it’s also been used by George Lucas’ Industrial Light and Magic (responsible for special effects in the original Star Wars trilogy) to manage its complex production process.
Unlike PHP, it wasn’t built for the web in mind, and there are no core web functionalities that are integrated into Python. Hence, we must use a web framework to develop web applications in Python. Web developers have started using it for the web since the rise of popular frameworks like Django.
What makes Python the go-to language for an increasing number of developers when it comes to web development these days? We’ll try to find an answer to this question in this post.
Why use Python?
Python for beginners
The primary reason for the popularity of Python is the elegance of the code — the brevity and readability in particular. For instance, let us look at how Python and Java stack up against each other in terms of reversing a number:
Programs to reverse a number in Java (left) and Python 2.7.x (right)
Python provides a short learning curve, making it ideal for beginners to learn. In addition to that, if your project is Python-based and new developers aren’t familiar with it, the transition is easier.
As Quora co-founder Adam D’Angelo says on the choice of Python for Quora’s development —
So far, we’ve been pretty happy with the choice … all of the early employees who’d been working with other languages in the past were happy to transition to Python, especially those coming from PHP.
In fact, the webcomic xkcd came up with a cartoon on how easy it is to get things done in Python!
Further, Python has easy to use debugging tools. Although there are several debuggers and IDE tools, the default one is pdb, an interactive debugging tool which allows a developer to stop the execution of a program midway and assess the environment to better understand run time errors.
Python on a remote server
The management of packages (or modules as they are called) in Python is very easy too. Use a package installer like pip or easy_install and it can be used to install and remove packages.
Python is very portable too! The ease of transferring your development environment to a remote machine is commendable. You just need to export the packages, and install it in a virtual environment on the remote machine with just two commands.
One more reason why Python is a good choice for web applications is the ability of running scripts which are not embedded to the web server (unlike running a PHP script.) Scripts are run as separate processes.
Web Development using Python Web Frameworks
Python’s web capabilities are not built into its core features, and therefore, we need the help of modules to enable web development in Python. If you want to develop very basic applications — such as a page for checking whether a server is running — you may use the cgi module.
Most of these frameworks need to be coupled with a web server like Apache for production purposes, and this is where a minor bottleneck is present. As compared to PHP, ease of deployment is a bit difficult on a remote web server if you are doing it for the first time in your life (although we have covered that as well). However, the time that you invest would definitely reap rewards later.
Django — the most popular Python Web Framework
A question naturally arises: Why use Python for web development? My answer is firstly to stop thinking of it as just being about using just “Python” for web development. It’s frameworks like Django and Flask that make web development in Python so feature rich and powerful. For instance, the following are a few things that Django does really well as compared to any other web framework:
- Easy Learning Curve. Just like Python, Django is easy to learn. You just need to go through this step-by-step tutorial on Django’s website, which does a great job of explaining the basic features of Django clearly. On the other hand, learning something like Ruby on Rails might take a longer time.
- Creating SQL tables automatically. Django does this for you once you’ve specified the structure. (Note: SQLAlchemy does the same, and there’s a heated debate about this on Hacker News, if you’re interested.)
- Generate Forms. Once you’ve created a Form class in Django and linked it to a model, the form generator in Django takes care of rendering forms, data validation and saving the data.
- Admin Interface. Just like the SQL tables, once you’ve decided the structure, Django creates an admin interface that allows you to manage the database. (It’s just like PhpMyAdmin built into Django.)
- Django Shell. The Python shell, within the Django project’s environment, is what the Django shell provides. This feature is useful in debugging, which is relatively more difficult in PHP.
If these are not enough, there’s cutting edge development going on in Python. Two exciting services in Python are Tornado (for asynchronous tasks) and Celery (for job scheduling). You may want to look at them if they fulfill your needs.
Django Web Hosting
The biggest hurdle for someone to start working on Django is the availability of web hosts. Obviously, due to the popularity of PHP, the number of hosts are high and hosting is relatively cheap (but not necessarily value for money, as cheap hosting is often shared). However, there are hosts like AWS and Heroku that allow you to host your Django applications for free (within certain limits). For shared hosts at a pocket-friendly price, you may go for a host like WebFaction.
The most popular cloud hosting services today like AWS, Digital Ocean and Microsoft Azure provide root access to the server, which allows you to manually set up your environment. Digital Ocean, in fact, has come up with one-click Django deployment. Django hosting might not be as popular as the PHP counterparts, but reliable options at different pricing levels are available.
The purpose of this article is not to tell you that Python is the solution to every problem! It is to highlight the parameters of Python and its web frameworks, which make life easier for web developers.
Have you tried Python and its web frameworks before? Do let us know in the comments below.
The Principles of Beautiful Web Design, 4th Edition
Docker for Web Developers
HTML5 Games: Novice to Ninja