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!