A Python replacement for PHP software ?

November 28th, 2008 at 5:26 pm

This blog runs on WordPress, and I’m quite happy with it. It’s simple to configure and use, is very well supported and the huge amount of plugins and extensions make every wish come true almost automatically. When we had to open a trave log, I didn’t think twice before using WordPress for that too. Regardless of the general theme of this post, I’m thankful for WordPress, and do think that its developers and maintainers deserve kudos on creating such a great piece of software, for free.

WordPress is implemented in PHP, like most popular web applications. In the world of (open-source) web applications, PHP is a 500-ton train sweeping everything else aside. Do you need a CMS ? No problem, have Drupal or Joomla. A photo-album ? There’s Coppermine (which I’m also using for our online photo album). Need a forum ? Everyone knows that phpBB rulez. And some of the big competitors, like punBB and Invision are also written in PHP.

There are literally truckloads of articles online explaining the popularity of PHP, so I won’t just drop another leaf into it. Rather, I just want to express an opinion that it’s a damn shame that this is the situation, because PHP is a bad language that encourages bad programming practice, which makes tinkering with large PHP painful.

I’m a natural tinkerer, and my WordPress blog is self-customized in several minor ways. I’ve also customized some of the plugins I use for my special needs. I can’t describe the disgust and horror I sometimes experience when fixing or changing something in WordPress’s PHP code. So much things are global – the variables, the hooks, the functions. Even after PHP added OOP, most code doesn’t use it, or only uses it in a very rudimentary way as module-like wrappers. PHP is probably an ideal language for applications to which many sub-competent hackers add code. It has almost no “accepted style” and people place no limits on what they do. This is a real shame.

I firmly believe that Python is a superior language to PHP. In all domains except web programming it’s also much more popular, so I’m really saddened by the lack of competition to PHP web apps from Python’s side. There’s absolutely nothing in Python that makes it less suitable for web applications than PHP – it’s simply a matter of custom.

Therefore, I think it would be really great if at least one of the big-gun web apps was written in Python. Maybe a competitor to WordPress, or a forum software. I, for once, would be delighted to use it once it’s stable enough. And it shouldn’t be too hard, either. All it would take is a single dedicated and talented Python hacker, perhaps in a setting like the Google Summer of Code. Take WordPress for instance. It weighs less than 30 KLOC of PHP code. Even assuming that PHP-Python translation is 1-to-1, brushing some of the more advanced features aside the Python replacement could easily be written in a summer. At first, it can mimic the WordPress architecture to make the transition easier. Then, once the project gains users and momentum, the code can be slowly and surely refactored to bring more good Python practices in.

I hope someone will start such an endeavor. Perhaps the Django team could offer it as a GSoC project for a WordPress replacement using Django (which would surely shave lots of code, as WP itself is written in pure PHP w/o any framework beneath).

Related posts:

  1. Book review: “Patterns of software” by Richard Gabriel
  2. A year with Python
  3. Book review: “Rapid GUI Programming with Python and Qt” by Mark Summerfield
  4. Book review: “Beginning game development with Python and Pygame” by Will McGugan
  5. Python

19 Responses to “A Python replacement for PHP software ?”

  1. Pradeep GowdaNo Gravatar Says:

    Zine, earlier known as textpress seems to be a good starting point for what you are looking for. Written using Werkzeug, Jinja and SQLAlchemy

    http://dev.pocoo.org/projects/zine/

    They already have importers for WordPress and Blogger implemented.

  2. Loïc d'AnterrochesNo Gravatar Says:

    Just a little remark with respect to “PHP is a bad language that encourages bad programming practice”, I have been dealing with Python code (Django apps for that matter) which where worse to maintain as correctly developed webapp in PHP. You can always torture a language and create unmaintainable bloat with it.

    Take a look at this piece of PHP code: http://projects.ceondo.com/p/indefero/source/tree/master/src/IDF
    This is a clone of GoogleCode written in PHP on top of a Django like framework (I program both in Python and PHP for a living) and take a look at an average Plone code.

    The language is not the problem, the problem is the brain controlling the keyboard.

  3. Carlo CabanillaNo Gravatar Says:

    I think the main reason why similar Python ports of PHP applications won’t catch on is hosting. You can get dirt cheap shared hosting for any PHP/MySQL applications that will perform just fine for the small scale that these PHP applications were written for. In order to host a Python application you’ll either need to make it CGI and deal will the performance hit that loading up the Python interpreter and all your app’s classes for each web request, or pay much more for a VPS or dedicated host to be able to run more persistent Python processes (fastcgi/mod_python/mod_wsgi).

    For the most part, users of blogs/wikis/forums/cms software don’t care much about customizing things, they just want something that’ll work. For that PHP is king.

    That said, as a Pythonista at heart, I would love to be proven wrong.

  4. Alexander ArtemenkoNo Gravatar Says:

    Also, read an this James Bennett’s article “Where is Django’s blog application?“, where he tries to explain why there aren’t any common Django/python based blogging application. His main point is that everyone writes his own blogging app in few minutes.

    Actually, about month ago I have started a project which goal is to provide reusable django blogging application. But it’s just an application and it’s not so easy to deploy if you don’t have your own django based site,

  5. Brian K. JonesNo Gravatar Says:

    I agree about hosting. I worked in an environment that hosted web sites for a large number of developers, and we only officially supported PHP because, from a sysadmin’s perspective, it’s a total no-brainer to set up and maintain. The reason for its popularity isn’t “custom”, it’s two things at work:

    1. An insanely easy-to-use language, and
    2. An insanely easy-to-implement/deploy language.

    I agree that this lowering of the bar in terms of skill required to get up and running creates some…. interesting code, but it also supports things like wordpress catching on like wildfire.

    I love django and would love to move in that direction more permanently for the web apps I create, but I still haven’t figured out the best way to deploy it, and nobody seems to have any straight answers. This is one area where it seems there is not ‘one obvious way’ to do it, and to be honest, it’s kinda driving me batty.

    If/when python becomes as brainless for sysadmins to deploy/support on the web as it is for programmers to code in, it will wipe php off the face of the earth.

  6. Michael WatkinsNo Gravatar Says:

    The big issue is the execution model. mod_python != mod_php for a number of reasons.

    Building a WordPress clone in Python isn’t the big challenge; solving the shared hosting deployment and scalability issues is.

    PHP through its implementation specifics (not least of which is that its standard library is all in C) makes it possible to serve up hundreds – depending on the machine, many hundreds – of virtual hosts all running their own instance of WordPress (or SquirrelMail or what have you). And if you make a change to a PHP source file, the very next http request delivers that to the browser client.

    The same relatively “easy” scalability is not the case with any of the current approaches to deploying any meaningful Python web app.

    Another major issue revolves around ease of use from a programmer, often beginning programmer, often beginning programmer with little or no Unix experience, perspective.

    To compete against PHP a Python “server pages” approach would have to make it possible for low budget shared hosting companies (as well as the big guys) to provide a competitive service to PHP which would allow any half brain-dead user to upload a .py or .html or .psp to their web root or below and then visit the page in a browser and see something happen. Something like:

    <?py
    print("Hello, world")
    ?>

    Needs to be brain dead simple to upload and see run.

    Currently that is far from the case.

    I’m not suggesting that is an ideal execution environment for serious applications; what I am suggesting is that unless an easy to deploy and relatively scalable execution environment is easy to install for *web hosting companies* (as opposed to end users, even programmer end users), you won’t see Python (or perl or ruby) driven projects take over from the big guns in PHP land like WordPress.

    On the other hand, a case might be made that for some applications such as forums that web hosts might actually benefit from deploying a good Python (or ruby or perl) based solution which uses an execution model more appropriate for the heavy amount of page and database accesses and updates such apps experience. A blog ought not to be a write-heavy app.

  7. Michael WatkinsNo Gravatar Says:

    Re my last comment, I see quite a few people jumped in with similar thoughts while I was off having breakfast.

    Loïc has a point – you can write crappy code in any language, although its also fair to say that some design features of Python encourage better code organization, such as namespaces (which I understand PHP has acquired after many years).

    Ultimately you are going to use the right tool for the job. If writing the next WordPress, something designed for mass adoption by all manner of users and web hosts, you’ll have to consider PHP. If writing the next identica.ca or reddit.com or twitter or blogger.com – centralized applications serving many users – then Python (and other languages and execution models) are excellent web development tools.

    (ps Loïc: anyone who uses emacs or vim to code PHP probably is farther down the road of experience than many PHP programmers).

  8. JohnMcNo Gravatar Says:

    Lets look at some advantages that PHP has over Python.

    1) Do you know C? Practically every CS grad has taken a C course if not multiple. There is no ramp up to use PHP. Add $ for variables and a few scoping rules and you are on your way. With Python the developer starts at ground zero again.

    2) Code base. Go look at phpsnaps or phpclasses. There are literally hundreds of code snippets ready to use. The remark about wrappers is right but that is the way the developer community wants it. An easy function call so they they don’t have to think about it. They just glue it all together with includes and those nasty global variables. THEY want it quick and dirty.

    Python can do this too. SQLAlchemy is a good example. The elegance of SQLA is neatly confined to just those imports necessary to get the job done. It is drop in ready to use with little helper code required to get to the meat of the problem.

    I prefer Python, but I will code in PHP too if the job calls for it. The difference in the two approaches is in the web environment each is expected to operate in. PHP grew up that way, Python adopted it.

    Web work is so loosely coupled that it invites a slap dash style of development. Don’t integrate that form code into the main page dude! Here just make it a pop up or a Ajax AHAH call. Oooh change the background color to match and lets call it a day. Boss wants that code up by lunch and I am getting hungry.

    But you know what? I wouldn’t despair too much. First of all if you want just a style of coding why not just use PSP server pages or Spyce? Simplicity and it works. If you want more elegance then why not webpy?

    Where I think Python will really come into play in the web world is thru Pyjamas. Getting up to speed on it now. Its ability to manage and utilize the browser DOM and jump between web work and a desktop environment has a lot to offer.

  9. Hans LellelidNo Gravatar Says:

    I’ve been a PHP developer for quite some time, including open-source work (Propel, Creole, Phing); however, I have to say that I completely agree that Python is a fundamentally superior language which really does allow you (and encourage you) to write much better code. And I would argue that well-architected, maintainable code is a fundamental requirement for a secure application.

    A language is just a language, sure. Just like a car is just a car. You can get from point A to B in any car, but I’d rather have a car where pieces didn’t start rattling and falling off everytime it gets on the highway.

    All my PHP code (for the past 6 years, anyway) has been object-oriented. I’ve written MVC frameworks, ORM tools, etc. Sure, you *can* write OO code in PHP and you can use Exceptions, but you are building on top of a language platform that clearly has no concept of OO and will issue unrecoverable fatal errors instead of throwing an Exception. It’s basically a huge mess and the new namespace stuff is just the comical final chapter in my own interest in PHP as a language. And don’t even get me started on thread safety, long ints, unicode, date/time values, or database abstraction.

    I’ll avoid posting more of a rant in this comment, but here are some links in case anyone cares to know any more about my particular PHP-based perspective on Python:
    - http://propel.tigris.org/servlets/ReadMsg?list=dev&msgNo=3480
    - http://creole.tigris.org/servlets/ReadMsg?list=users&msgNo=308

    Using Python has been a breath of fresh air, and I anticipate that once I become a little more qualified I’ll start doing open-source work in Python too. I’m particularly intrigued with the Paver project — and heck, if I can help with something like Zine, that might be lots of fun too. As evident in the Creole thread, I’m not the only one moving to Python. I only have anecdotal evidence, but it does seem that there is a growing trend among serious developers in the PHP world to start considering alternative platforms.

    -Hans

  10. markNo Gravatar Says:

    I am a ruby user myself since 5 years. I am ok with Python too. I dislike Perl and PHP.

    Now with that being stated, I think we must give PHP credit about being to strong in the www area, despite being such a horribly designed language.
    And, after we have acknowledged this, all the other languages should be horribly ashamed of themselves for being so incompetent to dethrone PHP seriously.
    (For me, ruby as a language has dethroned php years ago already after I wrote an IRC bot in ruby in some days, whereas I had no clue before this task at all how to do it in php. In my case the better language helped ME become better too. It was a huge paradigm shift. Anyway…)

    We also need to acknowledge that despite being such a horrible language, there are great PHP apps out there. My personal favourite is still phpBB, despite a few minor flaws. phpBB really is a joy to use and deploy.

    I believe to dethrone PHP the most critical aspect will be “key applications”.

    For example, trac for python is a killer app for the www. Django as well.

    But I dont really know a python-based forum kind that has any serious chance to dethrone phpBB. (Situation with ruby is very similar)

    In the long run I believe this situation will change, but for now PHP will still remain number one, despite the recent shrinking (btw the whole computer world as such still grows, so if a language like php shrinks then this means people would be actively moving away from it as a netto result)

  11. Michael FoordNo Gravatar Says:

    One possibility is Pinax – which provides reusable Django applications as components that can be plugged together as part of a bigger application.

    At work (Resolver Systems) we have a WordPress blog and a PHP BB forum. We are also in the process of creating a Django based web application. We have already hacked PHP BB to use Django authentication. To have Django applications integrated into the whole system that provided the blog and the forums would be really nice.

    Writing a complete blog or forum system is not trivial though. A solution that is 80% for some users is probably only 50% for other users.

  12. sapphirecatNo Gravatar Says:

    Agreed on the ease of PHP hosting. With no caches or moving parts, it trades power for ease of coding and administration. No URL routing, no persistent variables for one script run to affect the next one, no “weird complicated stuff” like MVC, and no way to alter the global Web server configuration. No worrying about spaces vs. tabs, and no headaches from line-ending conventions (something that bit me trying to write Perl in Notepad–we were all young once.) It all adds up.

    The worst part about it is that when one platform achieves dominance, it gets inertia too. You have to be much better than the dominant beast in order to win. Otherwise, few people will put up with the pain of the relative lack of apps for the technically better language, and fewer apps will be developed for it, and the cycle continues.

  13. Dmitry ChestnykhNo Gravatar Says:

    I too tweaked a few plugins for WordPress, and wrote a few myself, I prefer Python (and write desktop and web applications in it), but there’s nothing wrong with PHP — the problem is the programmer. I don’t think that those programmers who write *bad* PHP code will magically write better Python code.

    As for OOP, I want to point out the following article on Drupal OOP implementation:
    http://api.drupal.org/api/file/developer/topics/oop.html

    There’s nothing wrong with not using OOP, or not using OOP functions build into the language.

  14. Hans LellelidNo Gravatar Says:

    @Dmitry:

    I agree that there are also some very bad programmers who will write bad code in any language, but I disagree strongly with the suggestion that there is nothing wrong with PHP. I can give you a long list of things that are wrong with it, but I’ll mention just a few: no unified error system, no consistency in the core APIs, poor unicode support, no safe way to run in multi-threaded environment, and half-baked OO support (e.g. strict signatures, but no ability to overload), no namespaces or packages (may finally change with 5.3, but too little too late IMO).

    Hans

  15. Dr. GreenNo Gravatar Says:

    Hi there. Nice post. You should not so strongly tie PHP with WordPress. I have been coding in PHP professionally for several years and WordPress has got to be some of the worst code I have ever seen. Its had a long history of terrible security issues too. You do get some good PHP code, it just depends on who is writing it. To give you an idea, the lead architect of WordPress codes in Dreamweaver on Windows. Whereas good PHP programmers are on ‘nux and probably using an editor like jEdit. I even know a guy who codes all his sites in Vim.

    However. I 100% agree with you that Python is a superior language, despite the fact I continue to program in PHP. earlier this year I grew disillusioned with PHP and looked into other languages and came to the conclusion that Python was the best. Mainly due to everything being an object and multiple inheritance.

    The problem is, its like M$ vs. Linux and there is strength in numbers. Hopefully something will turn the tide though, like a replacement for WordPress in Python.

  16. cordNo Gravatar Says:

    I agree with Carlo and Brian (above).

    I’m not sure, but I suspect that both Python (mod_python) and Perl (mod_perl) suffer from the same affliction here. My novice’s understanding is that mod_{python,perl} provide access to Apache internals, while mod_php simply lets you run your php code in a persistent instance of the php interpreter. So, it makes it safe and easy for admins to provide mod_php, but not mod_{perl,python}.

    In defense of Perl and Python, I think the argument usually goes something like, “But with mod_{pl,py} you have much more power and control!”. Unfortunately, I don’t believe that’s what’s needed for mass adoption and ease of use: what’s needed is just for Apache to provide that nice sandbox that simply lets web devs have a persistent {Perl,Python} interpreter running.

    No idea why this hasn’t been done yet. Seems like a no-brainer, but maybe I’m missing something.

  17. ArunNo Gravatar Says:

    PHP and Python uses different models of web development. PHP uses the simple CGI model where each file is a mix of executable code and presentation logic. Easy to deploy, horrible to maintain and a nightmare for security.

    Python frameworks like Django/Pylons use a more evolved model. The logic is separated from presentation as much as possible. There is a tremendous flexibility in creating URLs and possibly RESTful web interfaces. The downside of this being some overhead in setting up the website.

    I am not sure if this can be simplified to the level of PHP. Some alternatives like using easy_install exists, but they are a far cry from the simplicity of the “drop the php files into a directory and point your browser”.

  18. thibauldNo Gravatar Says:

    Hi,

    I agree with you that PHP is far from being the most beautiful language out there. Python and Ruby are better designed languages but the fact is that Python or Ruby’s learning curve is a lot steeper. PHP could be compared to VB for the Web: easy to learn with great documentation all over the place and works.

    Let’s take an example here. Let’s compare how you remove duplicates from an array in PHP and a sequence in Python :
    PHP is easy :
    $array = array_unique($array);
    For Python however, you need a MS in computer science :
    seq = list(set(seq))

    Granted, this example does not work for Ruby (as easy as array = array.uniq ) but it was just to illustrate that beautiful languages are not necessarily the easiest to use. Plus the fact that PHP is completely web oriented and a rock solid language and you understand its success. Plus, you can write beautiful code with PHP too… it just requires good coders.

    I wrote a post about choosing the right software stack a while ago, you’ll find it here : Web application implementation step 1: Choose your weapons!
    Cheers,

  19. MagicmikeNo Gravatar Says:

    It is interesting that in over 2 years the situation has not fundamentally changed.

    Deploying Python is still a huge barrier to entry.
    – There is nothing as good as Ruby gems, or an easy way to bundle up and everything and deploy it so the dependencies just sort themselves out. This continues to kill Python on the web.
    This is to me the big problem. All others are second order effects.

    – There is very little motivation to code a web app in Python. I love it, but for 90/10 apps (90% web, 10% BI), Python is not the right choice. And that is sad. It should be.