Thonny debug AttributeError

Hi all.

I know I should be able to find the answer to this myself but I’ve tried and suspect this is one of those issues that is well known to all but us newbie numpties and so not documented in a way easily found.

I’m trying to debug some simple code in Thonny. The code uses the machine library and the pin attribute within. I’ve copied the machine.py file to the same folder as my code.py (which fixed the module error I was seeing).

When I debug I get the error:
AttributeError: module ‘machine’ has no attribute ‘Pin’
Sure enough, machine.py does not contain a pin function.

The code runs fine on the Pico.

Have I got the wrong machine.py on the computer?

Maybe my bigger more basic question is…where can I find a guide on how to set up Thonny to debug?

Help appreciated.

1 Like

Hi @Mark285907

Are you able to post the code that you’re trying to debug? Without having an idea of the code that is being looked at it is very difficult to be able to suggest a possible fix.

Thanks Dan.

import machine
import time

pir_sensor = machine.Pin(14, machine.Pin.IN)

def motion_detected(pin):
print(“Somebody here!”)

pir_sensor.irq(trigger=machine.Pin.IRQ_RISING, handler=motion_detected)

To be clear…the code runs in the Pico no problem. When I try to debug with Local Python selected as the port I get this error.

%Debug ‘2.10 Detect Human Movement - Part 1 as given.py’
Traceback (most recent call last):
File “F:\AA Orchard\aa Mark’s stuff\Pico\Kepler kit\Scripts\2.10 Detect Human Movement - Part 1 as given.py”, line 4, in
pir_sensor = machine.Pin(14, machine.Pin.IN)
AttributeError: module ‘machine’ has no attribute ‘Pin’

2 Likes

Hey @Mark285907,

The error message tells you that the “machine” module in your Python environment doesn’t have an attribute named “Pin”. The “machine” module you’re trying to use is specific to MicroPython, not your local Python environment on your computer.

The Pico runs on MicroPython which is slightly different to standard Python hence the difference in the outcome when this code is ran. Unfortunately, you can’t debug your MicroPython code using the Python debugger because they are different implementations. You can only run and test your code in the MicroPython environment. You might be able to use print statements or logging for some form of debugging though.

If you’re looking for a better way to debug MicroPython code, you could look into tools like mpy-repl-tool, a third-party interactive command-line tool for debugging MicroPython code.

The Thonny IDE has MicroPython support and allows you to debug MicroPython code running on a board.

Hope this helps!

1 Like

Thanks Sam. Much appreciated.

Yes, I’ve been using print statements to understand the MicroPython coding but that can be a long winded exercise. I’ve been debugging VBA in Excel since the '90s I assumed (wrongly) that Thonny would give the same functionality.

Re this comment

The Thonny IDE has MicroPython support and allows you to debug MicroPython code running on a board.

When I try to debug with the code running on the Pico the debug commands in Thonny are greyed out.

Hi @Mark285907

Looks like we’ve had a human moment there, by default the debug button will only appear when it is set to thonny’s local python environment, not when making changes to code on a microcontroller.

1 Like

Thanks Dan.

1 Like