Loading Python programs onto a Digi device

As demonstrated in the hello.py example, Python programs are loaded onto the Digi device using the Digi web interface page Applications > Python. Program files and modules can be uploaded directly to the Digi device’s file system through this page. However, files can be uploaded one at a time only, and there is no support for directory manipulation. This makes adding entire libraries of modules difficult and adding packages impossible. To address this issue, Digi provides a module called zipimport, allowing collections of modules and packages to be included in a single upload.

To decide which modules work well on the device, or to share information on your experiences, please check out the Module Notes page.

To use zipimport, add all the files to be moved onto the Digi device to a file named Python.zip. By default, Digi’s embedded copy of the Python interpreter checks for the presence of this file, and examines it when performing an import. Digi provides a Python.zip file which includes several useful Python standard library modules that are known to work well on our device. This file comes pre-loaded on your Python enabled device, or the latest version will always be available at Digi's product support website as part number 40002643.

Besides Python.zip, the zipimport module can be used with additional .zip files, provided the Python program knows of their presence and modifies its environment accordingly. Internally, the files accessed through the Applications -> Python web page are stored in a directory called WEB/Python/. To use additional .zip files, add them to the sys.path variable. This causes zipimport to search that file for .zip files as well. See the GPS sample application for an example.

To add mymodules.zip to the search path, you will need to following in your application.

import sys
sys.path.append("WEB/Python/mymodules.zip")

This is perfectly adequate and recommended for a production environment. However, during development it is often desirable to be able to load a modified version of a zipfile onto the device without rebooting. In order to do this, we must remove our zipfile from zipimport's directory cache.

The following code should allow you to load and reload mymodules.zip without rebooting.

                import sys, zipimport
zip = "WEB/Python/mymodules.zip"
if zip in zipimport._zip_directory_cache:
    del zipimport._zip_directory_cache[zip]

sys.path.append(zip)

 

© 2017 Digi International Inc. All rights reserved.
Loading Python programs onto a Digi device updated on 25 Sep 2017 08:55 AM