Search by Tags

Python in Linux

Applicable for

Compare with Revision

Subscribe for this article updates

Python Setup

With an Existing Linux Image

Python and related packages can be installed to an existing Linux image using the package manager (opkg). To see a list of relevant packages, enter the following commands:

opkg update
opkg list | grep python

Python (and any additional packages) may be installed with the following command:

opkg install python [optionally list additional packages...]

While Building a Linux Image

A set of python packages can be added to an OpenEmbedded Linux build for inclusion into the image's rootfs. After establishing the image build configuration, additionally append the following line to the oe-core/build/conf/local.conf file:

IMAGE_INSTALL_append = "python"

Then execute (or re-execute) 'bitbake' for the image (ie. 'bitbake angstrom-lxde-image'). For more information about building a Linux image, see OpenEmbedded (core).

Performance & Suitability

Tests results are pending; however, Python is expected to perform at least an order of magnitude slower than equivalent compiled C/C++ code due to Python's run-time interpreted nature. Use of a Just-in-Time or Ahead-of-Time compiler should improve performance; however, such compilers have not been tested with our Linux images.

Regardless, Python is still a viable alternative for performing lighter processing tasks. It can be effective for low level IO operations as well as high level applications such as web services. Furthermore, Python code can be rapidly modified and tested without the need to recompile.


GPIO - Blinking LED

Blinking a LED using GPIO access through sysfs.

#!/usr/bin/env python
import time
import os.path
GPIO_RESET    = False;  # Whether GPIOs should be re-exported
GPIO_PATH     = "/sys/class/gpio";
GPIO_DIR_OUT  = "out";
GPIO_VAL_HI   = "1";
GPIO_VAL_LO   = "0";
GPIO_CHAN_NUM = "146";  # GPIO1 on Apalis T30
BLINK_PERIOD  = 500;  # Blink period (milliseconds)
BLINK_DUTY    = 0.25; # Blink duty cycle (fraction)
def main():
        ### Initialize GPIO - optionally reset if already initialized
        ## Note: GPIOs which are already used in the drivers can not be controlled from sysfs, 
        ## unless a driver explicitly exported that particular pins GPIO.
        # Open GPIO export & unexport files
        exportFile = open(GPIO_PATH+'/export', 'w')
        unexportFile = open(GPIO_PATH+'/unexport', 'w')
        # Unexport GPIO if it exists and GPIO_RESET is enabled
        exportExists = os.path.isdir(GPIO_PATH+'/gpio'+GPIO_CHAN_NUM)
        if exportExists and GPIO_RESET:
        # Export GPIO
        if not exportExists or GPIO_RESET:
        # Open GPIO direction file to set direction
        directionFile = open(GPIO_PATH+'/gpio'+GPIO_CHAN_NUM+'/direction','w')
        # Set GPIO direction to "out"
        # Open GPIO value file to set value
        valueFile = open(GPIO_PATH+'/gpio'+GPIO_CHAN_NUM+'/value','w')
        # Loop indefinitely
        while True:
            # Set GPIO value to HI
            # Sleep for blink on duration
            # Set GPIO value to LO
            # Sleep for blink off duration
    except exception:
if __name__ == "__main__":

RTC - Read Time

Note: The modules fcntl and glob are provided by the packages 'python-fcntl' and 'python-shell' respectively.

#!/usr/bin/env python
import fcntl, struct, glob
# Identify RTCs
rtcList = glob.glob('/dev/rtc[0-9]')
print "RTCs: ", rtcList, "\n"
# Read each RTC
for rtc in rtcList
    #  struct rtc_time {
    #    int tm_sec;
    #    int tm_min;
    #    int tm_hour;
    #    int tm_mday;
    #    int tm_mon;
    #    int tm_year;
    #    int tm_wday;
    #    int tm_yday;
    #     int tm_isdst;
    #  };
    a=struct.pack('iiiiiiiii', 0,0,0,0,0,0,0,0,0)
    input=fcntl.ioctl(fo.fileno(), RTC_RD_TIME, a)
    result=struct.unpack('iiiiiiiii', input)
    print rtc + ": " + str(1900+result[5]) + "-" + str(1+result[4]) + "-" + str(result[3]) + " " + \
          str(result[2]) + ":" + str(result[1]) + ":" + str(result[0]) + " UTC"