Python as a PHP replacement?

by on Nov.30, 2009, under Python, Web Development

I recently sat down to coffee with a new acquaintance of mine who spends much of his time implementing F/OSS projects at non-profit organizations, and who had just stepped into a lead web developer position using PHP. After sharing pleasantries we began trading stories and talking about each of our “tools of the trade.” When I mentioned that I used to do most of my web development in PHP, but have spent the past year or so trying to move as completely as possible to Python, his response was: “Huh, I have never really thought of Python as a PHP replacement.”

Now, this guy hasn’t exactly been living under a rock for the past 10 years—his resume was quite impressive and included projects in a number of different programming languages; But as you can imagine, I was rather surprised by his response, and it made me wonder: Has the Python community really been that bad at promoting the strengths of Python for web development? Or, does the nirvana experienced by switching from a language like PHP to Python just make us so at peace with the world that we forget the hordes of developers still stuck with C-style syntax? Either way, it got me thinking about a few of the reasons why I decided to switch from PHP to Python; and why I not only see Python as an excellent PHP replacement, but am surprised it is such a “best-kept secret” for web development.

There are already plenty of pages out there discussing Python vs. PHP as a language, so I probably won’t get too technical here. I also want to try avoid turning this into a “Python is better than PHP because…” rant (for more on that, please see the end of this post), so I will simply share with you a few of the main reasons why I decided to replace PHP with Python as my primary language for web development:

» Python is more flexible than PHP

Python was engineered from the beginning to be a general purpose programming language. PHP on the other hand was written to overcome many of the “limitations” of the cgi-bin. This should say a lot about the “soul” of each language, but the conclusion I have come to is:

  • PHP is meant to do web things really well
  • Python is meant to do most things really well

Sure, these days you could write a desktop GUI in PHP, but it would most likely not be the right tool for the job (and you’d probably get laughed at). On a somewhat related note: many PHP developers I know find themselves switching between PHP for their website tasks, and a variety of other languages for other tasks (Shell Scripting for command-line scripts, Perl for text processing, etc.). As a Python programmer, I can typically stick to one programming language no matter what the task at hand requires (especially when armed with iPython).

Now, the obvious argument for PHP at this point is: “But wait, since PHP was written specifically for the web, shouldn’t that actually give it more credibility as the right tool for the web job?” Well, 5 or 10 years ago I might have agreed with you, but web development has changed a lot in the past few years (not to mention the introduction of Python’s WSGI spec).

The internet as a platform has grown and mutated drastically and now requires much more than “a server-side extension to HTML.” We are now in an age of full-blown internet applications, which require a true “application-level” language. In my opinion, PHP has only recently begun to move in that direction with the v5.3 release (as well as updates expected in PHP 6). That alone should give you pause when pondering PHP’s ability to “keep up” with the growth of the web.

In short, the internet continues to transform into an arena that is much less of a website platform, and much more of an application platform. To me, this makes a general purpose language such as Python the obvious choice for “future-proof” internet development.

» Python is typically faster than PHP, even especially on the web

You might find PHP beating out Python in a simple “hello world”-type test, but “real-world” applications will typically run much faster in Python. In a Web 2.0 world where everyone has broadband, speed is now an important factor in keeping your visitors happy.

In addition to “raw, under-the-hood” speed, Python’s language philosophy means it will typically scale better than PHP (although this can be argued a million different ways, since scalability usually involves much more than the language).

I’ve already done benchmarks comparing a few popular web frameworks, but if you’re still curious I’d suggest you dig around with Google for a while.

» Python is easier to read and maintain than PHP

Any veteran programmer will tell you that code readability plays a huge factor in code maintainability. With that in mind, Python was designed with some pretty strict syntax rules in regards to code-blocks and whitespace. While this may initially frustrate programmers who are used to a looser “bracket-encapsulated” language, it actually forces you to write more readable (and therefore, more maintainable) code.

Other than readability, Python’s clean syntax also means:

  • You can write more code with less errors (because they’re so easy to spot). This often comes as a pleasant surprise to programmers migrating from C-style languages (take Eric Raymond for example).
  • You end up typing less — No more semicolons or brackets, and fewer parentheses.
  • Your eyes don’t have to move around as much to figure out what’s going on.
  • Your first guess about what’s happening in a code-block is usually correct.

When you have time to kill, I’d recommend a stroll through Rosetta Code to compare how different languages “look” when tackling the same problem. Unless you just love brackets, I think you’ll find the non C-style syntaxes much easier to read.

» Python is more consistent than PHP

Continuing somewhat in the vein of readability is the topic of consistency. Since PHP’s lack of consistency has already been studied exhaustively by the Perl folks, I will spare you my rant and suggest you read theirs.

As a side note on this topic: I often hear PHP programmers talk about how awesome PHP’s documentation is in that you can open a web browser and just type “” to get help. Well, documentation recall usually isn’t that big of a deal for me (because I have a somewhat photographic memory), but I will say that after switching to Python I found myself turning to its documentation significantly less than I had with PHP, simply because I didn’t have to keep reminding myself whether that infrequently used function was named some_function or somefunction. With Python, the function/method I need is usually named exactly what I expect it to be named, and returns exactly what I expect it to return.

Oh, and don’t even get me started on PHP’s recent namespace decision.


» However, PHP still excels in a few areas «

Having said all of that, there are a few areas where I believe PHP may still shine brighter than Python:

» PHP hosting support is may be more prevalent than Python

It’s practically unheard of to find yourself on a web host that doesn’t support PHP out of the box. However, this is becoming less and less of an issue since most Python web applications can now be deployed using tools such as FastCGI or mod_proxy (as opposed to the popular mod_wsgi setup, which often requires root access to configure). These days any “reputable” web host is probably going to support some sort of strategy for deploying Python web applications. Of course, this only really applies to shared hosting environments without root access. If you’ve got a VPS or dedicated server you’re fine.

» PHP is arguably “more seasoned” on the web than Python

The communities behind projects like Drupal and WordPress really stand out to me as examples of the maturity of PHP on the web. Although similar Python web projects have been slowly emerging (and Plone has been around for years, if that’s your cup of tea), they don’t really compete yet in my opinion.

Of course, I am only talking about specific web projects here. Language wise, Python has been around longer than PHP. Web 2.0 framework wise, Python’s offerings have been around about as long as PHP’s (pretty much every language had its own answer to Ruby on Rails in 2005).

» PHP seems easier than Python at first (and might be for simple deployments)

There’s something magical about throwing together a few quick index.php/about.php/contact.php files and uploading them to the httpdocs directory of your webserver and it “just working.” A lot of simple websites have no need for acronyms like MVC, ORM, and DRY, and in those cases PHP is an excellent “server-side extension” to HTML. However, if and when you begin to scale, you’ll either find yourself switching to a full-blown framework, or throwing one together to help facilitate your growth. At that point, you’ll probably begin discovering the “dark corners” of PHP, and may find yourself wishing you had “done it right” the first time.


» So, which is better? «

Remember, the point of this post is to solidify the fact that Python can be an excellent PHP replacement. When it comes to programming languages, “better” is often more a case of personal preference than anything else. However, to me the points discussed above make Python the better choice when it comes to what I like in a web development language.

So, is Python a better web development language than PHP?
Well, does Nike make a better shoe than Adidas?

The most honest answer is probably: “It depends.” Python definitely has some significant advantages over PHP in certain circumstances, but the same could probably be said of PHP in other circumstances (although limited to the web I imagine).


I hope this post has encouraged you PHP programmers out there to take another look at Python. It may take some getting used to (Python’s way of doing things is inherently quite different than PHP’s), but you will probably find yourself thanking me later.

:, , ,

  • Dave

    Nice summary. How do I use python directly as a scripting language? Is there something similar to having PHP run by apache, or is there a little more to it?

  • Seth


    Thank you. Good question, and perhaps I will do a “part 2″ of this post going into more detail about that.

    In short I would suggest you check out a simple Python framework (Bottle is a great option) to familiarize yourself with how Python does things differently.

    Like PHP, the Python interpreter process can be run with Apache (using mod_wsgi). However, unlike PHP you will typically separate your “controller” and “view” code instead of writing HTML around <? and ?> type tags. While this may seem complicated at first, it actually lends itself to a better code layout. In PHP terms, it’s much like the process of using Smarty templates (PHP-only files as your “business” code, which then process and render your Smarty “presentation” code).

    Several Python template options exist, with the most popular ones probably being Mako (super-fast), Jinja (quick & simple), and Genshi (slower but XML-based).

    There are of course many options for deploying Python web applications, with mod_wsgi only being one of them. Typically during the building & testing phase, you will run your Python web app on some sort of built-in WSGI implementation and load it locally (if you’re familiar with Rails, it’s like running your Rails app locally and loading it on port 3000).

    Hope this helps!

  • V

    Interesting post.

    I’ve been interested in Python for a while now, having spent almost a decade with PHP. Seems a rather nice language and I can certainly see its benefits over PHP.

    However, I wonder about some things, for instance:

    1. Being developed for web, PHP has some inherent solutions for common requirements like session management, fcgi interfacing. Those are done in C, in the core of PHP, while Python requires middleware written in Python (e.g Beaker, Paster’s HTTP server or wsgi/fastcgi implementation).

    I wonder if there is a significant performance hit for Python because of that.

    It is true that there exists some theoretical threshold of app complexity above which PHP’s requirement to bootstrap entire application (the part thereof required to process the request) becomes significant performance bottleneck compared to load-once apps like those written in Python, which offsets any speed benefits of C code inside PHP. But I wonder how significant this is in practice? Even the most complex app has little to do in PHP. They all wait on I/O of whatever sort, even if the request generates dozens or more queries (take Drupal modules for example, at some point there can be over a hundred of queries per request). It is the stateless nature of HTTP that brings all languages to the same level requiring “third party” intervention like memory cached session management or data layers, database I/O, etc…, and number crunching algorithms can anyways be moved to C both in Python and in PHP. That being the common denominator, the true differences are then shown in parts that are done in the language itself: in this case the aforementioned session management, fastcgi interfacing (or in Python, entire HTTP server written in Python — Paster’s), etc…

    2. GIL worries me. While it is true that, especially for web applications, threads usually wait on I/O so GIL is not that much of a problem, I am not so convinced for practical implementations of apps that require performance and cache data wherever possible (reducing I/O wait). There, the GIL definitely impairs performance, but I wonder how significant?

    Also, SMP deployment with PHP is as simple as instructing PHP to spawn more processes. I’m assuming fastcgi implementation since Apache (and it’s mod_php) should not coexist with “performance” in the same sentence. :)

    It is as easy with Python (for instance using Paster), but each of those instances is interfaced with the web server through Python code, not C implemented code. Evenmoreso if proxying is used for load balancing.

    So if syntactic sugar present in Python is not sufficient to move us from PHP to Python (and in my case, I can name two: decorators and indentation/lack-of-$;{}, everything else is more or else the same), is there anything else that Python can offer?

  • Nilesh

    Thank you for this summary, I was searching for something like this – step wise differences between PHP and Python. I am a PHP Programmer but something clicked my mind that Python may be faster than PHP in webdev. And finally it is true what I thought :) Thanks again.

  • Julius H from San Jose, ca

    Wow! Thanks for the thorough explanation! Personally I am a bit of a newbie and was teaching myself python for application development, but wanted to eventually learn web developmen. So I thought I would have to eventually have to learn PHP to do this, reading this makes me more confident in Python and will save me time since I will not spend hours and hours of grueling study learning PHP. I will start from scratch figuring out how to deploy Python and hopefully by then the community will be bigger so I can ask around for meticulous question.

  • aardmaat

    great summary but I still have one question:
    for a while I’ve been thinking to switch to python on the web, because I already know some python for desktop development, but every time I try to do so, I get trapped into a world that’s the exact opposite from the ‘there should be one obvious way to do it’ philosophy:
    it seems like there are like a million ways to use python for web development, of which only PyHP seems a little promising since I only do simple sites. unfortunately I use things like XAMPP and WAMPP on windows for developing, and it seems like PyHP is no option for that either, so what would be the best way for me to transfer?

    I really like Python for desktop development, so I’d like to use the nice Python syntax for web development, but I also really love the way PHP can be mixed with HTML.

    thanks in advance

  • Alan

    V, I can understand where your concerns come from, but considering the performance difference between PHP and Python (hint: Python is faster), most of these aren’t issues at all.

    1. You’ll find that performance-critical parts of Python libraries are often implemented in C, also. In fact, Python makes this easier because you can treat C extensions to Python just like regular Python libraries. If you ever really need to optimize your code beyond what Python lets you do, you can always fall back to C.

    It’s usually better to optimize other things first, though. The language is usually the last things you need to optimize in any given scenario.

    You’ll also note that servers like Paster are usually put behind a reverse proxy (Apache, nginx or lighttpd) that takes care of static files and thus reduces the load on the app. Some are faster than others, too. For most websites, you probably don’t need to worry, though. Someone who should know these things once told me you can go a long way just using Python’s built-in wsgiref server to run your wsgi apps.

    2. The GIL shouldn’t worry you. While there is a lot of drama about it, you probably won’t encounter any such problems in practice. Most of the criticism is theoretical or only relevant for high-performance scenarios.

    3. If the prospect of cleaner, more straight-forward code and a much saner object model (first-class functions, etc) can’t convince you, nothing will. You should work with whatever language is easiest on your brain. For some people this is Python. For some people this is Ruby. Heck, for some people this is Perl.

    You should give it a try, if only for the sake of learning something new and keeping your mind fresh (it’s a good idea to try out new languages at least once per year to keep up with the times and broaden your horizon). Maybe you’ll like it. Maybe you won’t.

    You should also try Ruby. It has a lot in common with Python but is completely different (if only in its philosophy).

    This article seems to be aimed more at people who only stick with PHP because they don’t know of any alternatives. So if you use PHP because you made a conscious and educated choice, more power to you. But if you only stick with PHP because you haven’t written even a simple web app in another language, you should go and do that right now.

  • Markus

    “Has the Python community really been that bad at promoting the strengths of Python for web development?”

    -> Yes, somehow. It is really the marketing – this is the only part where we should learn from Ruby, how to hype and push something..

    -> And perhaps also a posterboy like David Heinemeier-Hanson is missing – instead of boygroup appeal our outstanding persons are more of the type “lumberjack” :) Just see this: :)

    I am just joking, but the truth is that the Python community sucks at marketing, and pushing more into the public.. It is frightening every time how less PHP and Ruby guys know from Python, and also how many misinformation is out there..

    Blog posts like yours, for example the benchmarks where people can see how performant web stuff in Python is running, helps, but sometimes it has to be more than facts.


  • Pavel Mocan

    Some guides for PHP developers that want to switch to Python would be extremely helpful and useful.
    Last night I was looking for what to use in Python for the PHP function isset() to check if request.POST (in Django) for an input exists or not … Luckily the #django IRC channel is very active :)Saying that, I might actually start compiling something myself and take a note of everything I need as I develop in Django. I’m doing a university project in Python/Django but haven’t really used them much before. My work is in PHP.

  • Nicolas Blackburn

    I love Python. I just can’t wait to build websites using Python. 

    There is only one problem: it’s not there. Most web hosting company’s have Python versions that are not up to date and/or are missing essential libraries like mysql drivers or image processing libraries.

    If you can afford a virtual host, you are somehow lucky, you can build what you need. But building the required libraries has always been a pain for me wasting many many hours of precious development time trying to figure out some cryptic and ungoogleable build errors.

    I honestly would love programming websites with Python, but lets be realist, it is rarely the best solution.

  • Gersande

    4 years later, is this still the case? I’m a programmer moving to Python from PHP, and I’m finding the versatility and simplicity of python to be truly extraordinary after years of PHP and WordPress and the limitations of both those infrastructures.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...