0

I am running MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico with RP2040 and I'm trying to import a module that I have written (called lcd_module). However, I keep running into this import error.

I am using VSCode and when I hover over the import lcd_module statement, the editor seems to recognise that lcd_module is indeed a valid module, so I'm not sure what's going wrong here.

Here is my directory layout:

my_name.py
lcd_module /
    __init__.py
    touch_lcd_lib.py

Within my_name.py:

import lcd_module

if __name__=='__main__':
    LCD = module.LCD_1inch28()
    Touch = module.Touch_CST816T(mode=1, LCD=LCD)

Within __init__.py:

from touch_lcd_lib import LCD_1inch28
from touch_lcd_lib import Touch_CST816T

And within touch_lcd_lib.py

from machine import Pin,I2C,SPI,PWM,Timer
import framebuf
import time


#Pin definition

# I have changed these to match my XIAO rp2040 pinout
I2C_SDA = 6
I2C_SDL = 7
I2C_INT = 29
I2C_RST = 0

DC = 26
CS = 1
SCK = 2
MOSI = 3
MISO = 4
RST = 27

BL = 28

#LCD Driver  
class LCD_1inch28(framebuf.FrameBuffer):
    def __init__(self):
        <omitted for brevity>

class Touch_CST816T(object):
    #Initialize the touch chip
    def __init__(self, ... <omitted>):
        <omitted for brevity>

To me everything looks good, but like I say, when I run my_name.py using the VSCode MicroPico plugin, I get

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'lcd_module'

When I tried using a similar directory structure with standard Python on some example files, everything seemed to be working fine, so I feel like I'm missing some MicroPython information about modules.

I also tried using relative imports. Chaning the import in my_name.py to

from .lcd_module import LCD_1inch28
from .lcd_module import Touch_CST816T

gives the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: can't perform relative import

and without the .s before lcd_module I just get the same no module named error as before.

Thanks for your help!

1 Answer 1

0

You probably need to manually copy your lcd_module directory (and the files in it) to the RP2040. Depending on how your version of MicroPython was compiled, the RP2040 might appear to the computer as a USB thumb drive (mass storage device), so you can copy files the same way you would copy files to a USB thumb drive.

Sign up to request clarification or add additional context in comments.

2 Comments

After some looking I figured this would be the way to go too, althought I'm not sure how to get the RP2040 to appear as a drive on my machine. For context I'm on Windows 10 and I used the latest Raspberry Pi Pico .uf2 file from the MicroPython website to get MicroPython onto my board. Any ideas?
OK, that's trickier, but you can use the Thonny IDE to upload files to the board. Or use mpremote (a command line utility written by the main developer of MicroPython), which has a cp command for copying file to and from the board.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.