ESP32 / ESP8266 MicroPython: Running a script from the file system

The objective of this post is to explain how to run a script from MicroPython’s file system. This was tested on both the ESP32 and the ESP8266.


The objective of this post is to explain how to run a script from MicroPython’s file system. This was tested on both the ESP32 and the ESP8266. The prints shown here were taken from the tests performed with the ESP32.

Note that this will be different from the previous tutorial where we ran a script from our computer on the MicroPython executing in a ESP. In this case, the script will be in MicroPython’s file system.

Naturally, this will be very useful for us to be able to execute Python code without the need to be repeating commands on the prompt.

This tutorial assumes a previous installation of the MicroPython support for the device used (ESP32 and/or ESP8266), and a previous installation of Python and ampy on the host computer. All the procedures shown here were tested on Windows 8.

The procedure

First of all, we will create a MicroPython script to upload to our ESP device. It will be very basic and we will only create an echo function that receives some content as input parameter and prints it.

We will also print a simple message, to show that both invocations of functions defined at the file and other functions from Python work well. Check the file script bellow.

def echo(content):
	print (content)

print("Running a script from the file system!")
echo("Invoking a function")

Save the file in a directory of your choice and with a .py extension. You can name it whatever you like, but for this example we will call it

Now we will upload the file to the ESP8266 / ESP32 using ampy. To do so, just open the command line, navigate to the folder where the file is and hit the command shown bellow. If you need a detailed tutorial on how to upload files with ampy, please check this post.

ampy --port COM5 put

Note that you need to substitute COM5 by the serial port where your device is. Also, if you used another name for the file, don’t forget to change it. Check the expected result at figure 1.

ESP32 ESP8266 MicroPython upload file with ampy

Figure 1 – Uploading the script file with ampy.

Now we need to connect to the MicroPython prompt to send some commands. I will be using Putty, but you can use other software of your choice. Once the connection is established, we will confirm that our file is on the file system.

import os

As can be seen bellow at figure 2, our is now on the file system, as expected.

ESP8266 ESP32 MicroPython list directory files

Figure 2 – Listing the files in the current directory of the file system.

Finally, to run the script, we just need to import it, since it will act like a regular Python module. Upon importing, all the executable statements will run, and the functions defined will be available.

import script
script.echo("Running the imported function")

As can be seen in figure 3, upon importing the module, we get the output from the executable statements we previously defined. Also note that repeating the import will not run those statements again. We can then access the functions defined in our file by calling filename.functionName.

ESP32 ESP8266 Importing MicroPython Module

Figure 3 – Importing the script file.


Related Content


Related Posts

This entry was posted in ESP32, ESP8266 and tagged , , , , , . Bookmark the permalink.

2 Responses to ESP32 / ESP8266 MicroPython: Running a script from the file system

  1. Pingback: ESP32 / ESP8266 MicroPython: HTTP GET Requests | techtutorialsx

  2. Pingback: CNXSoft – Embedded Systems News » Getting Started with MicroPython on ESP32 – Hello World, GPIO, and WiFi

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s