1

I'm using Thonny version 3.3.13 on Windows 10 to program Raspberry Pi Pico.

The main program is main.py. I have no issues with it (examples are working), except any local imports.
I'm following this tutorial.

It is not duplicated, as I've searched and tested many version of import on StackOverflow and many other websites for hours.

My file structure:

sd_card_read
  |-main.py
  |-lib
     |-__init__.py
     |-SDCard.py

My main.py file:

import sys
print(sys.path)

import SDCard

#... the rest of the code

The error I'm getting is:

['', '.frozen', '/lib']
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
ImportError: no module named 'SDCard'

How can I solve the import?

Notes:

  • I tried appending '.' and '/' to sys, it does not work. e.g sys.path.append('/')
  • I tried different versions of import, no luck. e.g from lib import SDCard
nekomatic
  • 5,988
  • 1
  • 20
  • 27
Binar Web
  • 867
  • 1
  • 11
  • 26
  • 1
    I just tried to reproduce your problem, and for me `from lib import SDCard` worked. Can you share the exact error message you're getting from that version? There is the possibility of name conflict, if Python is finding another `lib` out there to import instead of your local one. – joanis Jun 08 '22 at 12:46
  • 1
    Try changing the name of the `lib` directory to something more likely to be unique, and then try `from unique_name import SDCard` and report back if it makes a difference. – joanis Jun 08 '22 at 12:53
  • 1
    And a small note on naming conventions: in Python, the general practice is to use `all_lowercase` for the filenames, and `CamelCase` for the class names. See https://realpython.com/python-pep8/#naming-styles – joanis Jun 08 '22 at 12:55
  • No, I didn't try Thonny, and it's true I didn't clue in that it was key here. Just reading your answer, I see it's a detail that I should not have glossed over. My guess, if it can only load modules from its own internal storage, is that it might use a staging process when you run a file from your local computer. I.e., it might not actually run it on your computer, it might just transparently manage running as if from your computer to make your life easier. But now I'm just guessing, I've never used an RPi for anything. – joanis Jun 09 '22 at 12:42
  • I just added the `raspberry-pi` tag to your question: this is not a general Python question, it's a Python on RPi question, which is apparently quite different. – joanis Jun 09 '22 at 12:44
  • @joanis you're correct this is not a general Python question but it's also not a Raspberry Pi question - it's about MicroPython on the Raspberry Pi Pico microcontroller, which doesn't really have anything in common with the Raspberry Pi Linux-based computers. – nekomatic Jun 13 '22 at 14:18

1 Answers1

1

While Thonny allows you to run a file opened from your local computer, it ONLY allows importing modules from its own internal storage.

For me, this is confusing.

I ran "Save copy..." on all my module files, chose "Raspberry Pi Pico" and entered the filename manually.

Save copy...

Where to save to?

Save to Raspberry Pi Pico

Maybe there is another way of doing this in Thonny, as this is my first time using MicroPython on RPi Pico.

Binar Web
  • 867
  • 1
  • 11
  • 26
  • Does Thonny allow you to configure a whole project staging process? I.e., is there a way to script or configure copying or deploying all your files to the RPi in one script or click? For anything bigger than a toy project, that seems to me like an essential feature, so I hope the answer is yes. – joanis Jun 09 '22 at 12:49