Python and Django on Heroku

Listen to this article

Python has joined the growing ranks of officially-supported languages on Heroku's polyglot platform, going into public beta as of today. Python is the most-requested language for Heroku, and it brings with it the top-notch Django web framework.

As a language, Python has much in common with Ruby, Heroku's origin language. But the Python community has its own unique character. Python has a culture which finds an ideal balance between fast-moving innovation and diligent caution. It emphasizes readability, minimizes "magic," treats documentation as a first-class concern, and has a tradition of well-tested, backward-compatible releases in both the core language and its ecosystem of libraries. It blends approachability for beginners with maintainability for large projects, which has enabled its presence in fields as diverse as scientific computing, video games, systems automation, and the web.

Let's take it for a spin on Heroku.

Make a directory with three files:

import os from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello from Python!" if __name__ == "__main__": port = int(os.environ.get("PORT", 5000)) app.run(host='0.0.0.0', port=port)

Flask==0.7.2

web: python app.py

Commit to Git:

$ git init $ git add . $ git commit -m "init"

Create an app on the Cedar stack and deploy:

$ heroku create --stack cedar Creating young-fire-2556... done, stack is cedar http://young-fire-2556.herokuapp.com/ | git@heroku.com:young-fire-2556.git Git remote heroku added $ git push heroku master Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 495 bytes, done. Total 5 (delta 0), reused 0 (delta 0) -----> Heroku receiving push -----> Python app detected -----> Preparing virtualenv version 1.6.1 New python executable in ./bin/python2.7 Also creating executable in ./bin/python Installing setuptools............done. Installing pip...............done. -----> Installing dependencies using pip version 1.0.1 Downloading/unpacking Flask==0.7.2 (from -r requirements.txt (line 1)) ... Successfully installed Flask Werkzeug Jinja2 Cleaning up... -----> Discovering process types Procfile declares types -> web -----> Compiled slug size is 3.5MB -----> Launching... done, v2 http://young-fire-2556.herokuapp.com deployed to Heroku To git@heroku.com:young-fire-2556.git * [new branch] master -> master

Then view your app on the web!

$ curl http://young-fire-2556.herokuapp.com/ Hello from Python!

Dev Center: Getting Started with Python on Heroku/Cedar

Created by Guido van Rossum in 1991, Python is one of the world's most popular programming languages, and finds application in a broad range of uses.

Cutting-edge communities, like Node.js and Ruby, encourage fast-paced innovation (though sometimes at the cost of application breakage). Conservative communities, like Java, favor a more responsible and predictable approach (though sometimes at the expense of being behind the curve). Python has managed to gracefully navigate a middle path between these extremes, giving it a respected reputation even among non-Python programmers. The Python community is an island of calm in the stormy seas of the programming world.

Python is known for its clearly-stated values, outlined in PEP 20, The Zen of Python. "Explicit is better than implicit" is one example (and a counterpoint to "Convention over configuration" espoused by Rails). "There's only one way to do it" is another (counterpointing "There's more than one way to do it" from Perl). See Code Like a Pythonista: Idiomatic Python for more.

The Python Enhancement Proposal (PEP) brings a structured approach to extending the core language design over time. It captures much of the value of Internet standard bodies procedures (like Internet Society RFCs or W3C standards proposals) without being as heavy-weight or resistant to change. Again, Python finds a graceful middle path: neither changing unexpectedly at the whim of its lead developers, nor unable to adapt to a changing world due to too many approval committees.

Documentation is one of Python's strongest areas, and especially notable because docs are often a second-class citizen in other programming languages. Read the Docs is an entire site dedicated to packaging and documentation, sponsored by the Python Software Foundation. And the Django book defined a whole new approach to web-based publishing of technical books, imitated by many since its release.

In some ways, Python was the birthplace of modern web frameworks, with Zope and Plone. Concepts like separation of business and display logic via view templating, ORMs for database interaction, and test-driven development were built into Zope half a decade before Rails was born. Zope never had the impact achieved by the later generation of frameworks, partially due to its excessive complexity and steep learning curve, and partially due to simply being ahead of its time. Nevertheless, modern web frameworks owe much to Zope's pioneering work.

The legacy of Zope's checkered history combined with the Python community's slow recognition of the importance of the web could have been a major obstacle to the language's ongoing relevance with modern developers, who increasingly wanted to build apps for the web. But in 2005, the Django framework emerged as a Pythonic answer to Rails. (Eventually, even Guido came around.)

Django discarded the legacy of past Python web implementations, creating an approachable framework designed for rapid application development. Django's spirit is perhaps best summarized by its delightful slogan: "the web framework for perfectionists with deadlines." Where Rails specializes on CRUD applications, Django is best known for its CMS capabilities. It has an emphasis on DRY (Don't Repeat Yourself). The Django community prefers to create reusable components or contribute back to existing projects over single-use libraries, which helps push the greater Python community forward. While Django is a batteries-included framework, the loose coupling of components allows flexibility and choice.

Other frameworks have found traction as well. Flask, a Sinatra-like microframework, makes use of Python's decorators for readability. Pyramid emerged from the earlier Pylons and TurboGears projects, and their documentation already offers excellent instructions for deploying to Heroku.

Similarly, Python established a pattern for webserver adapters with WSGI. Many other languages have since followed suit, such as Rack for Ruby, Ring for Clojure, and PSGI/Plack for Perl.

Perhaps most striking about Python is the breadth of different realms it has taken root in. A few examples:

Science and math computing, evidenced by books and the SciPy libraries and conferences.

Video games, as seen in libraries such as PyGame and Cocos2d.

As an embedded scripting / extension language, in software such as Blender3D, Civilization IV, and EVE Online (via Stackless Python).

Major Linux distributions use Python for their system tools, such as yum and the Red Hat Network client for Red Hat and Fedora; or almost all of the GUI configuration and control panels on Ubuntu.

It's one of the three official languages used by Google, alongside Java and C++.

And of course, internet startups: Reddit, YouTube, Disqus, Dropbox, and countless others use Python to build their businesses.

We anticipate that Python will be one of the most-used languages on the Heroku platform, and are overjoyed to welcome our Python brothers and sisters into the fold.

Special thanks to all the members of the Python community that helped with alpha testing, feedback, and patches on Heroku's Python support, including: David Cramer, Ben Bangert, Kenneth Love, Armin Ronacher, and Jesse Noller.

We'll be sponsoring and speaking at PyCodeConf next week. Come chat with us about what you'd like to see out of Python on Heroku!

Further reading: