In addition to storing data files on the SD Card, you may also want to store Python scripts or libraries. This is especially useful for the "non-Express" boards that have a very limited file system.

In order to execute code from the SD Card, you have to tell the system where to look for the executable files.  The search path is set and viewed via the sys.path command. The default path looks on the processor file system in "/" then "/lib" then in the modules contained in .frozen as part of the board's CircuitPython build. Here is an example:

The search path may be extended via the sys.path.append command. If you have followed the previous example for mounting an SD Card as "/sd" then you can extend the search path using the sys.path.append("/sd") command as shown:

If you want mount the SD card and extend the search path via a script, place the following code in a file named sdmount.py and then copy it onto the file system of your processor board (not the SD card).

In this example the script sdmount.py will mount the SD card as "/sd" and then extend the sys.path to use "/sd".

This example is written for the Feather M0 Adalogger so the "chip select" is set to SD_CS. For other boards, use the pin that is connected to the Chip Select line of your SD card (e.g. board.D5).

# SPDX-FileCopyrightText: 2018 Jerry Needell for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import sys

import adafruit_sdcard
import board
import busio
import digitalio
import storage

# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
sys.path.append("/sd")

The file sdmount.py  must be placed on the processor file system, not on the SD card. For a non-express board like the feather_m0_adalogger used in this example use ampy.

ampy -p /dev/ttyUSB0 put sdmount.py

From the REPL you can now reboot (by typing Control-D) then mount the SD card and verify the path:

hello.py Example

The following walks through a simple example  of how to execute a script from the SD Card.

First, store the following code as hello.py  on the SD Card using your computer. Typically this would be done by removing the SD Card from your device and attaching it to your computer where it will be mounted as a USB drive. You can save the hello.py file directly on the drive or save it locally on your computer and drag/drop it to the USB drive.

# SPDX-FileCopyrightText: 2018 Jerry Needell for Adafruit Industries
#
# SPDX-License-Identifier: MIT

print('Hello, World!')

After copying the file to your SD Card, and reinserting and remounting the SD Card on your device, you can verify that the file is there:

As you can see, there are some additional files on my SD Card.

You can now execute the code from the SD Card like any stored file as shown:

If you want to have access to the entire Adafruit CircuitPython Bundle, you can copy the lib/ subdirectory to your SD Card then make it available with an additional append. Here is a revised script sdmount_lib.py file that also appends "/sd/lib" to the path.

# SPDX-FileCopyrightText: 2018 Jerry Needell for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import sys

import adafruit_sdcard
import board
import busio
import digitalio
import storage

# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
sys.path.append("/sd")
sys.path.append("/sd/lib")

Copy sdmount_lib.py to your processor's File system (not the SD Card).

ampy -p /dev/ttyUSB0 put sdmount_lib.py

You now have access to the entire Bundle! You can see that '/lib' shows up first in the path. When searching for a library the system will first look in /lib then in /sd and /sd/lib.

For example, you can load the NeoPixel driver with:

import neopixel

and it will be loaded from the SD Card.

This guide was first published on Sep 21, 2016. It was last updated on Sep 27, 2017.

This page (Code Storage on SD Card) was last updated on Nov 29, 2023.

Text editor powered by tinymce.