Find out what it takes to install Python, run a basic “Hello World!” script and get started with your own scripts on the N95.
You may have seen my previous article about how to run Python scripts on the N800, located here. While the N800 ships with built-in Python, there are a few hoops that have to be jumped through in order to run scripts. The N95 doesn’t have built-in Python, but it’s easy to install, and once you’ve got it, it’s even easier to run your scripts.
In this article, I’m going to show you how to get Python installed on your N95 and run a basic “Hello World!” script. Then I’ll point you to a few resources for learning more about Python and the N95.
First, you may be asking, what is Python? Python is an open source programming language developed in 1991 by Guido van Rossum. It’s object-oriented like Java and quite powerful, but unlike Java features a very concise syntax that many programmers prefer. You’ll notice that you can write a rather powerful Python script with surprisingly few lines of code. But if you are used to Java (or scripting languages such as PHP), you’re going to have to get used to a slightly different way of coding. (Lines don’t end with semi-colons, for example, and you’re required to use proper indentation.) Let’s start.
Installing Python
Go and grab the Python file first. Download onto your computer the files called “PythonForS60_1_4_1_3rdEd.SIS” and “PythonScriptShell_1_4_1_3rdEd.sis”. Send them to your N95 with Bluetooth and run each of them to install. This should be fairly painless.
If you go into your Application folder, you should see an icon labeled “Python” with a weird plus-sign shaped icon made of stylized blue and yellow snakes. Select that and you’ll see something that looks shockingly like a command prompt. It is, actually. If you hit Options and choose Interactive Console you can type Python commands right there! (It may help to use a Bluetooth keyboard, if you have one.)
Try typing these two lines:
a = 10
print a
(Note: No semicolons. Another note: Pushing the middle button in your 4-way rocker, the one you select menu items with, does a carriage return.)
You should see:
10
Surprise. Yes, you can script live right in the console. You wouldn’t necessarily use this approach in most instances, but it could come in handy; we’ll explore it a bit more later in this article.
If you’d like to see more of what Python can do, it comes with a few examples. Select Options and then Run Script. There should be a few available. Poke around. These are good examples of the flexibility of Python on the N95.
When you’re done, get out of Python altogether. We’re going to write our “Hello World!” script.
Hello World!
Here’s our script:
import appuifw
appuifw.note(u’Hello World!’, %27info’)
Short, eh? Just copy this and save it in a file called “hello_world.py”. Bluetooth that over to your N95. I’d recommend saving it somewhere easy like “e:/Python” (if you have a memory card). Python seems to have some difficulty finding files that are saved in unexpected places on your phone.
Once you do this, launch Python, select Options and then Run Script. Now, “hello_world.py” should be in the list. Select it.
“Hello World!”, it says! Great.
So what is this?
Well, the first line imports the library that handles the user interface elements such as pop-up alerts, forms, buttons and such. And “appuifw” means “application user interface framework.” The second line creates a “note” element (a pop-up, really) with the text “Hello World!” in the style of an “info” box. You could also put “error” or “conf” (as in “confirmation”) in there to get different icons.
The Bluetooth Console
As shown above, one cool thing about Python is the ability to code live. You can use the console to type commands that execute as soon as you hit return. Instead of typing on the small phone keyboard, though, you can use the Bluetooth console and code live on your phone by typing on your computer, which can be very cool.
On a Mac, you can do this: Load your Terminal application and type “screen /dev/tty.Bluetooth-PDA-Sync”; this connects you to the Bluetooth serial port. Now, go to the Python app on your N95. Choose Options and then Bluetooth console. It’ll ask how you want to connect. Choose your computer and then the “Bluetooth-PDA-Sync” port. It’ll ask if you want to set this as the default, which you’ll want to do.
(Windows users should check this article out. It’s a bit more involved, unfortunately.)
When you’ve done this, you should see something like this on your computer screen:
Connected.
Python 2.2.2 (#0, Oct 11 2007, 20:32:16) [C] on symbian_s60
Type “copyright”, “credits” or “license” for more information.
Type “commands” to see the commands available in this simple line
editor.
>>>
If so, great! You’re communicating with your phone.
Try typing in those lines from above on your computer:
a = 10
print a
It should give you:
10
Perfect.
Now, try pasting in the hello_world.py script:
import appuifw
appuifw.note(u’Hello World!’, %27info’)
If you did it right, it should look exactly like it did when you ran the script on your phone. A “Hello World!” info box should pop up on the phone.
And there you go.
Learning More
Python’s a pretty powerful language that allows you to write sophisticated scripts with very little overhead. Remember, as well, that Python isn’t just a language for mobile devices. Web servers run it. You can write apps for your computer with it. All sorts of stuff is possible. So it’s worth exploring if you enjoy code.
If you’d like to know more about Python on the N95, make sure to check out these links:
- Jurgen Scheible’s Python for S60 tutorial
- Nokia’s Python for S60 wiki
Do you have some ideas for Python and the N95 that you’d like to contribute? Share them in the comments section.
6:16 AM
12.01.08