Is Thonny Reporting the Wrong Micropython Program in Error?

1,084 views
Skip to first unread message

Paul Naish

unread,
May 30, 2023, 1:28:31 PM5/30/23
to thonny
I'm asking the question this time because I originally thought I'd found a bug in uPython and create a post in their forum. You will need to look there for the details but the net is that Thonny 4.0.1 seems to incorrectly report the error on BOOT.PY when the actual error is in BOOT_X_PARAMS.PY to import levels down.

In Thonny when I run BOOT.PY the reported in the SHELL is

>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
    File "<stdin>", line 3, in <module>
ImportError: can't import name boot_x
>>>

The problem is with boot_x as shown below.

When using the uPython utility mpremote the results from a soft_reset are:

$ mpremote
Connected to MicroPython at /dev/ttyACM0
Use Ctrl-] or Ctrl-x to exit this shell
>>> import machine
>>> machine.soft_reset()
MPY: soft reboot
Traceback (most recent call last):
  File "boot.py", line 3, in <module>
  File "boot_x.py", line 3, in <module>
  File "boot_x_params.py", line 4, in <module>
NameError: name 'params' isn't defined
MicroPython 699477d on 2023-01-16; Raspberry Pi Pico W with RP2040
Type "help()" for more information.

Now, when I run the above mpremote steps in the shell window of Thonny I get the same results so the problem only seems to occur running the program from the IDE window

MicroPython 699477d on 2023-01-16; Raspberry Pi Pico W with RP2040
Type "help()" for more information.
>>> import machine
>>> machine.soft_reset()
MPY: soft reboot
Traceback (most recent call last):
  File "boot.py", line 3, in <module>
  File "boot_x.py", line 3, in <module>
  File "boot_x_params.py", line 4, in <module>
NameError: name 'params' isn't defined
>>>

So am I doing something wrong running from the Thonny IDE?

Paul Naish

unread,
May 30, 2023, 1:44:38 PM5/30/23
to thonny
Opps. I said "The problem is with boot_x as shown below." when I meant to say "The problem is NOT with boot_x as shown below.". Which there was an edit for posts

Aivar Annamaa

unread,
May 30, 2023, 3:57:27 PM5/30/23
to thonny
Which code do you run in Thonny? Is it the same code that is saved to boot.py on your device? Does this program import "boot_x_params.py"? How have you configured your intepreter in Thonny -- does it have "Restart interpreter before running a script" turned on? If it is on, then "Run" is supposed to restart MicroPython (which makes your device run boot.py) and then send the editor content for execution via REPL. When this option is turned off, then "Run" simply sends current editor content for execution via REPL.

The idea behind Thonny's "Run" approach is to allow testing the main code (the code which is supposed to end up in main.py) by editing a local file. Only after you're happy with the code, you would export it to your device as main.py. I agree, that this can be confusing, especially as the other locally saved modules won't automatically participate in this scheme -- when the code sent to the REPL imports anything, the modules must be present on the device.

The alternative approach is to always save the code on the device and test by pressing Ctrl-D for the regular soft-reboot.

I'm open to suggestions for making this situation less confusing.

Best regards,
Aivar

Paul Naish

unread,
May 30, 2023, 5:37:30 PM5/30/23
to thonny
Hi @Aivar. As to your questions
  • [Which code do you run in Thonny?] All code is on the PICO W
  • [Is it the same code that is saved to boot.py on your device?] Yes. See the attachment for a full listing of the PY files.
  • [Does this program import "boot_x_params.py"?] 'boot_x_params.py' is imported by 'boot_x.py'. 'boot_x' is called by 'boot'
  • [How have you configured your intepreter in Thonny -- does it have "Restart interpreter before running a script" turned on?] >> Yes
With 'Restart interpreter' set, which I did for the test, then 'RUN BOOT.PY' I get the error 'ImportError: can't import name boot_x'

With 'Restart interpreter' turned off, 'RUN BOOT.PY', I get the same error.

When I say 'RUN BOOT.PY' I using RUN from the Thonny toolbar.
Code.txt
Reply all
Reply to author
Forward
0 new messages