3-Lib banner
>>Smartphone TV (watch now)
  >>Series 60
Help keep 3-Lib going with PayPal

Help keep 3-Lib going with PayPal

B2 Stealth

Radio controlled planes for the park!

Cheap, high quality memory cards, recommended!

The incredibly small Utilikey

PIX badge/clock/banner

Swiss Card multi-tool!


Introduction to Python on Nokia/Symbian Series 60

Python logoThe story so far 

(First published on AllAboutSymbian, Feb 2006)

As regular readers may know, Ewan and I, apart from being fellow AllAboutSymbian scribblers, are equally devoted to OPL, a system for producing palmtop and smartphone applications without all that tedious mucking about with C++ and SDKs. OPL worked well, the applications we produced worked well and all was rosy for over a decade. But then came Series 60 and some of the vital tendrils which kept OPL working harmoniously with Symbian OS were (unintentionally) broken by Nokia. The result was that Series 60 has effectively been a closed door to OPL and attempts to re-establish contact have so far come to nothing.

Meanwhile, Nokia themselves had recognised the need for some kind of Rapid Application Development Language (RAD), a way for people to produce useful programs without having to spend months learning Java or C++ and the multi-threading intricacies of Symbian OS. Spying the Open Source Python language, they presumably preferred it over the equally open OPL because Python was at heart Internet-focussed and more flexibly structured, better suiting the needs of a port to a connected smartphone.

Like OPL, Python needs its software system installed on any device on whicb the Python program is desired to run. Unlike OPL, there's no need for a translation stage (with a runtime running a 'translated' version of the program). With Python, the raw text-based code is run ('interpreted') directly on the device.

In terms of ease of getting started though, both systems are similar. The Python for Series 60 package includes a number of useful, working example programs (e.g. a camera interface, an image viewer, a Snake game, a file manager, etc), which means that only a couple of minutes after starting, you can be running any of these example Python programs on your smartphone - and examining the code in your favourite text editor, to see how they work.

Some of the example scripts/programs The example image viewer in action The example Snake game

Comparing Python to other systems

The obvious question is "How does Python compare to XXX?", where XXX is OPL, C++ or Java. Rather cleverly, it has many of the best attributes of each of these.

Like OPL, it's possible to develop anywhere you can edit a plain text file, running directly on the smartphone itself. Like OPL, the functions are high enough in level that you don't have to worry about the intricacies of threads, buffers and pointers - on the whole, you simply call the modules and functions needed and leave the rest to the Nokia port of Python to implement on-the-fly.

Like C++, Python's based around the idea of classes and objects. Thus, rather than spell every function out in detail, you can set up an object (like a screen or sound engine or a menu) and then rely to a large extent on it knowing how to behave when referred to. I'm a complete novice in 'object-oriented programming', so this is one area I'm still very much learning.

Like Java, Python is very much a cross-platform thing, although it doesn't pretend to be 'write once, run everywhere'. It's more 'write once, and then tweak it without too much effort for any other platform'. Python's been ported to Windows, Mac OS, Unix (where it started, of course), Palm OS, Windows Mobile, and now, of course, Series 60 on Symbian OS. All the core modules are available on each port, although each platform usually has extra modules and functions, to take advantage of the unique features of the platform or of the hardware it's running on (e.g. an Internet-connected smartphone).

OPL is the most obvious comparison, especially in view of my experience with it. THE main feature of OPL was that it was possible to create working programs very quickly without having to go on training courses and without needing a brain the size of a planet. And hundreds, if not thousands of home, would-be programmers did just this with OPL, in the mid 90s on Psion palmtops and later to a lesser degree on the Nokia Series 80 Communicators and on UIQ smartphones.

Can Python be used in the same way? Will it take off and create a whole user-led enthusiast community in the way Symbian's David Wood has forecast? Well, the potential's certainly there. Admittedly Python doesn't ship in any smartphone's ROM (as OPL did in Psion's heyday), but it's fairly easy to add and there are vast numbers of Python-programming geeks (like me, hopefully) who can step in to add the application 'that they need' and then open it up to others.

What's the catch?

Now, before we get too carried away, Python isn't going to be the best language in which to implement any Symbian/Series 60 application. Like OPL, there's just too much overhead in terms of RAM (typically about 1.5 Megabyte) and in lost processor performance through having to go through the Python interpreter (though note that if there's more than one Python program running, they share the interpreter, so the RAM hit is limited). So we won't be seeing extra Office applications, graphics suites or route planners. But there are thousands of other niche applications which have yet to be created for Series 60.

Python's also not the best language for implementing a commercial (or crippled shareware) application. The plain text of your application's code is right there for everyone to see. Even when packaging a Python script in standalone form (see below), your code is all there in 'default.py' in your relevant \system\apps\myapp\ folder. Anyone can copy and paste this and adapt it and there's little you can do to stop it. (Yes, there's an obfuscator, but it's limited by definition in what it can do since at the end of the day the Python interpreter's still got to run the script.) So Python's best suited to custom programs, for specific interests, or, of course, for freeware and fully working shareware (yay!), where there's no real incentive for anyone to want to try and rip off your work.

It's also worth looking at the code itself, as the focus and style is very different from OPL. To give an example, to send an SMS from OPL, you'd have to dig out a specialist OPX (OPl eXtension) and jump through hoops, if it could be done at all. In Python for Series 60, you simply use

sms_send(recipient, message)

Similarly, to take a photo using the smartphone's camera, you'd use


When OPL was conceived, this sort of functionality wasn't dreamed of for the Psion palmtops, so you can't blame OPL for being too limited. Now, the modules and functions available in Python for S60 v1.3.1 aren't totally complete, and there are various holes, but these should be filled in quickly now that Python/S60 is Open Source.

Partly redressing the balance, I'd say that Python's slightly harder to get to grips with for the first time (than OPL), if only because of the object-orientation used throughout and because of the sheer scope of its functions. As an Open Source language on other platforms, Python has acquired an awful lot of libraries and modules...

Overall, though, and especially because of the non-appearance of OPL for this most successful Symbian OS-based platform, I'd heartily recommend Python for home, custom or lightly commercial projects.

Taking your first steps

After installing the Python interpreter on your S60 smartphone, you'll immediately see a handful of example scripts and you can run them directly on your device. This will prove to you that Python's working, at least.

The next step is to create your own program, which is the exciting bit. Either by modifying one of the examples in your favourite text editor (I like TextPad), to get a feel for how real world code looks, or by working through Jurgen Scheible's little tutorial series. I can strongly recommend printing off (or keeping open) the official 'API_Reference_For_Python.pdf' that comes with the Python coding kit.

Initially you can just beam your new script/program (i.e. the .py file) over to your smartphone, where it will be picked up by the PyS60 recognizer and you simply choose to 'install as script' or 'install as library'. For most purposes, you'll want to choose 'script'. Then start up the Python interpreter and run your installed script. Pretty easy!

Nokia's API guide

Creating a distributable application

Where things start to get a little more complicated is in trying to create a Python program that appears to the user as just another S60 application. As part of your Python coding kit, you'll have acquired a little command line tool called Py2sis. As the name suggests, this takes a Python script and turns it into a SIS file for installation elsewhere. All the user needs to do is accept it in the usual way. Oh, and have their own installation of the Python interpreter, of course.

In the same way as with OPL programs, it's enough to have a handy download link to the Python files page on your own download page, so that users who don't currently have Python can upgrade their smartphone. Python's a bit of a beast to install in that it's 500K and takes a minute or so for Symbian OS to physically process, so you really, really don't want to include the interpreter in the SIS file along with your tiny, efficient, 10K application.

Getting down and dirty for more ambitious applications

But what happens when you want to include a proper S60 icon, or include multiple files, such as images and sounds? To achieve this, you'll need to get your hands dirty slightly. For a start, you'll need Nokia's full (150MB) C++ SDK for S60, although you really only need it for some extra command line tools:

Create yourself a pair of S60 application icons in .BMP format (44 by 44, 256 colours, plus its 2-bit mask, and 42 by 29, 256 colours, and its mask). The masks can simply be 2-bit versions (i.e. black and white) of the icons themselves - professionals will hand craft these to stop rogue pixels looking 'wrong', but simply saving as black/white is a good first cut at a mask. Next, combine your four .BMP files using BMCONV. For example, I use a BMCONV batch file for my example 'musician' application, created for the purposes of this introduction:

musician.fil contains:


Next, you need a UID for your application, a unique identifier so that your application never gets confused with someone else's. You can get one from Symbian here. You'll need to quote it in making up a special RSS file, such as:

// musician.rss
#include <aiftool.rh>

        CAPTION { code=ELangEnglish; caption="Musician"; },
        CAPTION { code=ELangFrench; caption="Musician";}   // to work round an AIFTOOL bug, you need to have at least
                                                                                              // one language mentioned here

// Replace the following app_uid with your program's UID.

// The number of icons (not including masks)

Use AIFTOOL to combine your combination .mbm file with your .RSS file, producing an .AIF (Application Information File) that's going to tell Symbian OS which icon to display on its menus.

Now the hacking really starts(!) Run Py2Sis, as above, but with the flag '--leavetemp'. This takes your .py code and constructs an .APP file, and leaves it, with a .PKG file describing how to make the final SIS file, all in a ./temp folder for your further use. You now have both .AIF and .APP and things are looking promising.

Assuming that, like me, you have several ancillary files that you want packaged in with your Python application, you need to copy them into the ./temp folder manually. Then edit the PKG file to reference these extra files (plus the aforementioned AIF one) and make a copy of this in your root folder so that it won't get forgotten when you zap the ./temp file later on. I've found it convenient to do these last few steps in a command line batch file:

py2sis musician.py --leavetemp
copy *.mid .\temp                             ! my extra files
copy musician.pkg .\temp                 ! my PKG file, corrected for my extra files and the AIF one
copy musician.aif .\temp                    ! my application icon file
copy 3-Lib.cer .\temp                         ! optional developer certificate
copy 3-Lib.key .\temp                        ! ditto....
cd temp

Finally, run MAKESIS on the PKG file, producing a SIS file that contains your app, its icon, its files, etc. Job done. Phew!

I should perhaps note at this point that if you get baffled by any of this (as is likely, at least at first) that you SHOULDN'T email me directly. The best place to ask Python for Series 60 questions by far is the official (free) Forum Nokia discussion board.

Musician's menuThe proof of the pudding

So, the code all written and the hard work over, what does a standalone Python application look like? Well, something I've been after for ages was a utility to help tune my guitar (and my daughter's violin) and also display different tempos (i.e. a metronome) - both the existing (C++) S60 applications in this field don't appear to work. So what better test of Python?

So.... [drum roll] presenting Musician v1.4, all my own work (apart from all the lines I cribbed from a couple of Jurgen's examples!) and created in around ten man hours from 'never having seen a line of Python before' to 'finished and ready for download by the world'. Download it here.

Interestingly, with my OPL experience, I could have coded the same thing in one hour flat for UIQ or Series 80, but the learning curve for Python has definitely been worth climbing, with its potentially more powerful features and the fact that OPL isn't yet available for S60.

Musician's menuComments on the application are welcome, as is comment on how I could improve this introduction to Python. Meanwhile, I'd urge you to think of an application that you want written and to attempt if yourself in Python. Start with one of Jurgen's templates or examples and build it, step by step, learning Python along the way. Or, if you're the sort of person who likes doing things strictly by the book, there are a number of official Python tutorials online, although there will be modules not available in Python for Series 60 and you won't get quite the same degree of instant gratification of seeing your ideas come to life on your smartphone.

If you create an application in Python that's worth sharing, by all means comment here on AllAboutSymbian, drop me an email and also share it on the Forum Nokia board.

You're not going to become rich through writing Python programs, the plain text code puts paid to the idea of being too precocious about your Python ideas, but there's every chance you might get famous as the creator of something genuinely useful. Get your thinking cap on!