Saturday, 12 January 2013

2.2.4 Writing and Running a Python Script


2.2.4 Writing and Running a Python Script

1. Write a Python Script
You can write a Python script on your computer with any text editor.
Useful editors for Windows are, for example, ConTEXT or PyWin which
are freely downloadable on the Internet, but Notepad also works. Possible
editors for Mac are, for example, SubEthaEdit, TextMate or BBEdit. We do
not recommend using Mac’s TextEdit application, as it might place some
invisible characters in your code which throw an error when executing
the script. In Linux, you can use Vim or Emacs.


Write the following line in your text editor:
print 'Hello world!'
After the code is typed, save the file under the name hello.py. Make
sure that the file ending is .py and not .txt.
The file is now ready to be executed on your phone! You do not have
to build or compile it any way. However, it must be copied to the phone
first.


2. Install a Python Script on a Phone
You send a Python script to the phone in the same way that you sent the
installation files in Section 2.2.3.
Open the received hello.py file in the message inbox by selecting it.
The message inbox handler will recognize it as a PyS60 file and display
a popup note. When asked to install your file as a ‘Python script’ or a
‘Python lib module’ (Figure 2.10), choose ‘Python script’. Your Python
script is automatically installed to the correct place. Now you can test the
script.


3. Test a Python Script
Start the PyS60 interpreter by clicking on the Python icon (Figure 2.1)
on the desktop or inside the appropriate subfolder on your phone. Once
the PyS60 interpreter has started up, select ‘Options’ (Figure 2.4(a)) and
‘Run script’ (Figure 2.4(b)). Choose your script name, hello.py, from
the list and select ‘OK’. Your script should now start up and you should
see a green line stating Hello world! (Figure 2.5).
Congratulations! You have successfully written and executed your
script with PyS60! To go through all the examples in the book, just repeat
steps 1–3 for each new script. Have fun with it!













No comments:

Post a Comment