VPK (Virtual Python Kit)

What? The idea for this module is to add Python functionality to Drupal using 'virtual python', so enabling Drupal to make use of Python even in shared hosting which does not offer Python, and where user doesn't have SSH access - i.e. usable almost anywhere Drupal can be installed.

Is it feasible? Yes - for proof of concept I've tested setting this up manually in shared hosting which does not offer Python.

Why? There is a huge range of mature Python modules and applications which could be leveraged by Drupal once VPK is installed. Python is a great language with many powerful features which can make developing modules and advanced functionality quick and easy. VPK could then be a foundation allowing modules to take advantage of the 'batteries included' power of python.

Some illustrations of what could be easily done using VPK/Python just for admin utilities:

  • Easy install / update of Drupal modules - fetching the update, md5 checking, backing up, extracting etc.
  • Easy applying patches - perhaps don't even have to get the full contrib module, using VPK, it would be possible to just use the patch to do the updating instead.
  • Easy backup of Drupal site and database, and could also login to a secure online file hosting account and transfer the backup there.
  • Easy set up of multisite configurations, dev, test, prod setups etc. - creating symlinks, directories,
    databases, etc.
  • Keep two Drupal installations on different servers synchronized.
  • A lot of great stuff is already available in Python - check out the Python packages: http://pypi.python.org/pypi?%3Aaction=index

    If these aren't enough, then Python can also act as a wrapper to access libraries in C etc.

    It may also encourage Python developers to make use of Drupal, and hence potentially widen the community.

    How does it work? The module would include it's own bundled Python interpreter, which would be used to set up and install a virtual python environment using virtualenv.py. Once setup, this temp copy of python interpreter in the sites/all/modules directory would be removed. The module would also install Python setuptools (allowing very easy fetching, installation and updating of python packages). The Python environment created would be a sandboxed version which could be specifically configured for Drupal (and not effected by changes to Python and packages made by the web hoster or even the user's own version of Python).

    Considerations:

    VPK could be quite big - potentially over 5Mb.

    Licensing - Python is GPL compatible, so that's ok, but some modules aren't - so have to be aware of that when developing modules using VPK. Virtual Python (virtualenv) itself however is under MIT permissive license (i.e. license notice has to be distributed, otherwise can be modified, distributed, sold etc.). This may be an issue as it's not GPL?

    Security - THIS IS THE BIG ONE! Python is very powerful and could be used to do all kinds of malicious things. Security should be a very big concern. For this reason, some shared hosting may not be suitable for VPK (i.e. if suEXEC, scgi or similar isn't available, or not have .htaccess rights). Expertise on security issues and how best to address these will be essential to move VPK forward.

    Drupal Guidelines - this could possibly involve an additional raft of considerations. i.e. practice for using Python scripts, coding syntax best practice guidelines. (perhaps like what was done with Javascript).

    Where VPK couldn't be used. Some quick 'proof of concept' tests with a couple of shared hosting providers suggests that this works in principle on standard non-Python enabled freebie hosting. The limitations are primarily security related. It might not be a good idea to have VPK installed under the public_html directory, and some freebie hosting accounts limit directory access. (there are plenty of freebie shared hosting options which don't have this limitation).

    Other related projects

    There's a few interesting projects which are related and highly relevant:

    pybridge: http://www.freenet.org.nz/pybridge/ (Drupal module which is framework allowing Drupal page-generating modules to be written in Python) This of course requires Python to be installed, so VPK and pybridge would be complementary. (be great if pybridge development were taken further).

    Python Services API: http://drupal.org/project/python_services (A set of classes and and tests that Python developers can use as a basis for integration with the Services module.)

    Conclusion:

    It's doable, and shouldn't be too-hard to do, and could add enormous functionality to Drupal.

    Login to post comments

    The module would include

    Garrett Albright - Tue, 2009-06-16 18:04

    The module would include [its] own bundled Python interpreter

    So a pre-compiled binary?

    It sounds like there's a lot of places for compatibility issues and troublesome edge cases with this - as there always is whenever you try to get cheapo shared hosting accounts to do something other than just serve web files. And certainly the security concerns are troublesome, as you acknowledge. But don't let me discourage you…

    (If you wanted to be truly hardcore, write a Python runtime in PHP!)


    Compatibility issues

    dannz - Wed, 2009-06-17 08:38

    Compatibility issues shouldn't be too much of a problem - most Drupal is hosted on Linux or Windows, and any hardcore FreeBSD, QNX, Unslung users would have ability to simply run the virtualenv script.

    A stripped down version of the linux x86 would work for over 95%, and since the core of the interpreter is less than 800kb, a Windows version could be bundled too. The bundled interpreter only had to do the job of running the script to build the full Python installation. An alternative would be to simply fetch the right interpreter for the OS during installation.

    I'm not sure what you mean by compatibility issues as the the linux interpreter will work on any linux distro running on an x86, and the windows version on any windows on x86 (you can also cater for windows Pocket PC, Palm OS, iPhone if you want as well - but I don't think we have to worry about these troublesome edge cases too much to begin with).

    You don't have to get the cheapo web hosting to do anything - they don't have to tweak a thing - it all gets done by the user installing this Drupal module.

    Doesn't discourage me - I've found that setting up a virtual python environment works very well, in fact much better than relying on the web hoster to offer Python (and having to wait for them to install modules etc.).

    I also came across this: 'Python in PHP': http://www.indelible.org/php/python/guide.html "This extension allows the Python interpreter to be embedded inside of PHP, allowing for the instantiate and manipulation of Python objects from within PHP." I haven't tried it out, but it may make it easier to think of it putting PHP on steroids rather than having another language.

    It's true there may be some PHP puritans who may feel everything ought to be in PHP - well, they can have the fun of writing a Python runtime in PHP, or simply disdain using VPK or anything requiring it. For others, this would give the benefit of fast byte-compiled code with powerful OO and ability to do a great deal more very quickly (isn't extensibility a key principle for Drupal?).


    This would be very useful

    kyle_mathews's picture
    kyle_mathews - Fri, 2009-07-03 21:00

    This would be very useful for the Memetracker module which has a python dependency.

    Kyle Mathews