Friday, 13 April 2012

programming and moodle - pycode installation

Pycode is a quiz module for Moodle 2.1 (and, I hope, later versions, though it's tested only on 2.1), which lets us ask Python programming questions. The student's answer is a bit of python code that is run on the server for validation, with a given set of testcases. If the code passes all tests it gets full marks, otherwise it gets nothing and the student has to resubmit. The questions are intended to be run in Adaptive Mode, so that there will be a penalty for each failing submission.

The Moodle code is a more-or-less standard quiz question module, though due to a deficiency in the Moodle 2 architecture I've also had to write a special quiz question "behaviour" module to install. These two modules are available from two git repositories ( and and installation is just a simple matter of copying the php source code into appropriate places in the directory tree.

The student supplied code obviously has to be run in a sandbox that prevents unauthorised system calls and excessive use of memory or cpu time. The sandbox I use is pypy -- see appears to be very safe; several smart people here have tried to hack it unsuccessfully and if you read the webpage you'll probably see why. However, it's a bit of a pig to install.

To avoid problems with constant stream of pypy updates, and to add a little bit of my own code as a front end, I've forked the base pypy distribution; my fork is available as a mercurial distribution at Unfortunately, this has to be built from source. The steps are:

Get the source from the Mercurial repository into a directory /usr/local/pypy-sandbox-4-pycode
Install all the dependencies as per ... ng-the-pypy-python-interpreter
cd /usr/local/pypy-sandbox-4-pycode/pypy/translator/goal
python -O2 --sandbox
[After about half an hour] mv pypy-c /usr/local/pypy-sandbox-4-pycode/pypy/translator/sandbox/pypy-sandbox

One major issue should be mentioned: do you run SElinux? If so, there's an issue with running Python from the webserver (which is necessary to provide the front-end to pypy). It's a known Python/SELinux issue (e.g. see ... numpy-from-mod-wsgi-django-app or An SELinux expert is apparently able to solve the problem by first turning on logging of SELinux AVC denials then adding a specific exception to allow the offending operation. However, no-one here is clued up enough to do that -- we just turned off SELinux, but you may not wish to do that.

So -- too daunting for you? Let me know if you wish to proceed.

Notes that I am making on the install

this is all done on a ubuntu install with LAMP stack.

all run from ~
sudo apt-get update

sudo apt-get install libssl0.9.8
sudo apt-get install gcc
sudo apt-get install python-codespeak-libsudo apt-get install python-ctypes

need to install
sudo apt-get install mercurial
and run
hg clone

sudo mv pypy-sandbox-4-pycode/ /usr/local/
cd /usr/local/pypy-sandbox-4-pycode/pypy/translator/goal
python -O2 --sandbox

wait half an hour to four fours for install

sudo mv pypy-c /usr/local/pypy-sandbox-4-pycode/pypy/translator/sandbox/pypy-sandbox

moodleinschools2.1 install from
unzip the pycode downloads that are listed at bitbucket
pycode need to go into /moodle/question/type

the adaptive files need to go into /moodle/question/behaviour

I am amazed that I have now a working pycode when doing the unit tests Site Administration > Development > Unit Tests. You then enter "question/type/pycode" into the box "Only run tests in ...".

Note: I had this working at 2pm, but did not read the email from the developer correctly, which stated, You then enter "question/type/pycode" into the box "Only run tests in ...". Had I done this I would be further along the develop of questions.

One thing I have had to do is to install xdebug

The next thing is how to setup a quiz question, it looks simple enough, but I think I need to see an example of it.
Seeing examples of the questions and trying to get them working seem to be a bit of a challenge for me at the moment, looks like I may have to get started on learning python.

No comments: