Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.
micropython pico external modules featured image

Adding external modules to MicroPython with Raspberry PI Pico

4.3
(44)

Last Updated on 2nd September 2023 by peppe8o

In this tutorial I’m going to show you how to include your custom, external modules in MicroPython with Raspberry PI Pico.

Importing external modules in Python is a common way to include custom functions in your code, keeping it ordered and clean. The import statement is available also in MicroPyton but requires the right files positioning to make it working with Raspberry PI Pico

The Libraries Path

raspberry pi pico microcontroller

A common problem for RPI Pico new users with MicroPython, when using your external .py modules, is getting errors like “ImportError: No module named”. This error claims that external modules are not located where MicroPython is expecting them. This is the easiest problem to solve. Before showing where to copy your modules, let’s understand how it works.

Unless specified differently, MicroPython looks for external modules using “sys.path” variable. This path can be shown in your Thonny IDE (please refer to my first steps with Raspberry PI Pico tutorial to get started with Thonny) with following 2 commands in your interactive shell:

import sys
sys.path

This will show something like the following:

>>> import sys
>>> sys.path
['', '/lib']

This answer means that MicroPython uses the root path (”) and its “lib” subpath (‘/lib’) as default folders where modules are to be stored. Please note that these paths refers to RPI Pico memory, so you have to store them in your board. The only exception is with built-in libraries, that are already available without explicit files and can be listed with command:

help('modules')

So, when you add to your code a import statement to include a custom library, let’s say for example a “import mylibrary”, the interpreter will look for “mylibrary” file, appending “.py” extension, in its default folders.

It will result successfull if it finds “mylibrary.py” file under root or “/lib” folder.

Showing Files and Folders with Thonny

With Thonny IDE, you can easily browse your RPI Pico and Computer folders. You can get this enabled from Thonny’s menu “View” -> “Files” option. This flag will enable the following folder three on left window side.

If Raspberry PI Pico is already connected, you will see files stored in it (“main.py” and “mylibrary.py” in following picture):

Thonny view files menu

The one shown in picture is a correct external library positioning. If you have many external libraries, you can also order them within a “lib” folder (in files area, inside Raspberruy Pi Pico section, right click with mouse and use “New directory…” to create a new one).

Adding Custom Library Path

You can also add your own custom folder including modules to be enable for importing. From your shell (or from your main script, to make it persistent on each reboot) use following commands, where “mylib” can be whatever you prefer as folder name:

import sys
sys.path.append('/mylib')

Create in your Raspberry PI Pico a folder named exactly as appended on sys path.

You can now use root, lib or your custom folder (mylib in last example) for importing external modules in MicroPython:

>>> import sys
>>> sys.path
['', '/lib', '/mylib']

What’s Next

Interested to do more with your Raspberry PI Pico? Try to look at my Raspberry PI Pico tutorials for useful and funny projects!

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 4.3 / 5. Vote count: 44

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

4 thoughts on “Adding external modules to MicroPython with Raspberry PI Pico”

  1. Hey Giuseppe,
    I was importing requests module(library) for HTTPS requests. But it is not available for pico-micropython-image. How can i use a package which is not
    not for micropython in my code.

    1. Hi Vaibhav. It is impossible to use a “Not MicroPython” library in MicroPython, unless a cross-interpreter is developed. But, in my opinion, a cross interpreter would reduce the performance of a microcontroller and would not make sense. If you need to use a specific module, you must install the firmware with the same language for that module.
      By the way, what is your need? What is the goal of your project?

  2. Having zero luck with the Pi Pico W …went to the forums and was relieved to find was not the only
    one. The ESP8266 seems to have none of the associated headaches off this board, so I’ll stick with it
    If you want my Pico WI ‘ll mail it to you for free if you want it LOL

Leave a Comment

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy

Subscribe my newsletter:
×