With this post I'm starting a new series on nice Python tricks which might be interesting to tablet software developers. So, if you're doing something in Python on your tablet, this series is for you!
In this first episode I will talk about writing relocatable software. This is software which works no matter where it's installed. There are no hardwired absolute paths to resources in relocatable software.
Knowing where you are
Every Python module knows the path where it's installed. You can retrieve the module's path by reading the __file__ variable.
print "This module resides at:", __file__
This makes it easy to find the directory where your app is installed:
import os
path = os.path.dirname(__file__)
Finding your resources
So if you want to load bundled resources into your application, you know where to find them:
path = os.path.dirname(__file__)
image_file = os.path.join(path, "images", "foo.png")
img = gtk.Image()
img.set_from_file(image_file)
Starting the application
This is the way I use to make my programs executable on the tablet. All of the program files and resources are within a subdirectory (e.g. /usr/lib/myapp/) together with the executable start module, which could look like this:
#! /usr/bin/env python
from mediabox.App import App
app = App()
app.run()
Let's assume this file is /usr/lib/mediabox/MediaBox. Then I make a symbolic link to this file as /usr/bin/MediaBox, and it's all done.
If I move my software to another place, I would just have to update this link.
1 comment:
Nice tutorial. I've put a link to this post at PyMaemo (documentation page).
Post a Comment