Basic import question

125 views
Skip to first unread message

Steve Ryan

unread,
Feb 18, 2023, 7:41:22 PM2/18/23
to thonny
I seem to me missing some basic information, and I have spent hours looking for an answer.

If course I can run this code.  It works the same whether the file is on my PC or on the Pico W.
def say_hello():
   print( 'Hello, world, no import!' )
   
say_hello()


But this does NOT work when split into two files stored on the PC:
In script.py:
import mymodule
mymodule.say_hello()


In mymodule.py:
def say_hello():
   print( 'Hello, world, files on Windows!' )


The error message is:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'mymodule'


However, if I copy the same two files to / on the Pico, they run fine.  It also works if script.py is on the PC and mymodule.py is on the Pico.

1) Why is it different on the two platforms?
2) How can I make this work for files on my PC.  This is going to be a fairly large project, and I'd rather minimize the number of files stored on the Pico.

I am new to the Pico and to Thonny, but I have been programming for years, mainly in C and Java with some Python more recently.  I have rarely been stumped by something so seemingly simple.

Thanks for any help.
Steve


John Lucas

unread,
Feb 19, 2023, 9:33:54 AM2/19/23
to thonny
Python needs a main.py file.  On the Pico this is run every time the controller is powered up.  The usual way to do what you want is like this:

In main.py

from  myModule  import MyModule

def main():
    mm = MyModule()
    mm.say_hello()

if __name__ == "__main__":
    main()

In  myModule.py:

Class MyModule():
     def say_hello():
          print( 'Hello, world, files on Pico!' )

This is quite similar to Java and makes it easy to handle complex programs coded in many small classes.

You are new to Pico so be aware of this.  If your program goes into a loop you will do what I did and cut power.  Restart and main.py will run and you are back in the  loop.  Re-install the micropython/circuitpython on your Pico.  Main.py will still run when you restart!  Make sure you have flash_nuke.uf2 handy so you clean clean everything off the Pico so you can do a fresh restart.

This creates another problem as your code is wiped by flash_nuke.   Make sure you take regular copies of your code from the Pico to PC.  The only way I could see to do that is to copy each file using save/as.  You then have to save/as or reload from the Pico otherwise Thonny is pointing to the PC and not the Pico.  Anybody know how to do a copy all?

Cheers

Steve Ryan

unread,
Feb 19, 2023, 1:49:44 PM2/19/23
to thonny
Thanks for replying.  I tried your changes, and everything works the same as a before.  I'll try using your approach in the future.

Unfortunately, my question is unanswered.  Why can I run one file from Windows using Thonny, but not 2?  I'd really like to keep my code repository on the PC, and only move what is necessary to the Pico when it's time to test (that is, when I hit the run button).

Perhaps that is just not how Thonny and the Pico work, but I'd still like to understand.  It seems to be that this is a file management issue rather than a coding issue, and I don't know what the assumptions are.

Steve

Aivar Annamaa

unread,
Feb 20, 2023, 2:30:32 PM2/20/23
to thonny
In MicroPython mode (i.e. when a MicroPython variant and a port is mentioned in the lower-right corner of Thonny's main window), "Run current script (F5)" command always sends the code in the active editor to be executed on the device, even if you store the code in your local filesystem (see https://groups.google.com/g/thonny/c/z78gZQbVOs4/m/1youR4mbBAAJ for mode detailed explanation). This means, all the modules required by this code must exist on the MicroPython device.

If your program consists of several modules, then you need to either edit the helper modules on your device, or keep them in the local disk but do a "File => Save copy ..." each time you want to test your program and the helper module on local disk has changed.

Best regards,
Aivar

Steve Ryan

unread,
Feb 20, 2023, 8:45:00 PM2/20/23
to thonny
Thanks!  It had to be something like this, but I didn't understand as a new users.

Steve

Reply all
Reply to author
Forward
0 new messages