Raspberry Pi Pico ImportError: no module named 'machine'

Question:

I am running the following blink program on my raspberry pi pico. I am using circuit python.

from machine import Pin
import time

led = Pin(13, Pin.OUT)
while True:
    led(1)
    time.sleep(1)
    led(0)
    time.sleep(1)

When I run it though it gives this error:

Traceback (most recent call last):
  File "code.py", line 1, in <module>
ImportError: no module named 'machine'

I have tried to find if I need to download a library file or any thing about the machine module, but I have found nothing. If you know why it can’t find the machine module that would be greatly appreciated.

Asked By: Jonas Parsons

||

Answers:

Your code is for micropython. Circuitpython is different. See here https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out

from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.LED)
Answered By: aMike

I think you are using the incorrect uf2 file as the official (stable) version is not out as of writing this.
In order to check this, type the following in MircoPython’s command line:

import sys
sys.implementation
(name=’micropython’, version=(1, 19, 1), _machine=’Raspberry Pi Pico with RP2040′, _mpy=4102)

If the response shows "Pico" and not "Pico W" then copy the latest version from here and copy it onto your Pico W (in USB mode)

Answered By: RobbieZA

I was having same issue and a search found this thread.
I changed the Interpreter setting in Thonny (under options) from Local Python 3, to MicroPython (Raspberry Pi Pico)

Answered By: Duncan