Fix “ModuleNotFoundError: No module named 'machine'” in Thonny
Your code ran on the PC’s Python, not on the board. Select the MicroPython interpreter in Thonny (bottom-right) — don’t pip install “machine”.
Community draft — report a mistake
What you see
Python 3.10.11 (bundled)
>>> %Run blink.py
Traceback (most recent call last):
File "C:\Users\Asha\Documents\blink.py", line 1, in <module>
from machine import Pin
ModuleNotFoundError: No module named 'machine'Python 3.10.11 (bundled)
>>> %Run blink.py
Traceback (most recent call last):
File "C:\Users\Asha\Documents\blink.py", line 1, in <module>
from machine import Pin
ModuleNotFoundError: No module named 'machine'
machine is MicroPython’s hardware module (pins, PWM, I2C, ADC). It lives in the firmware on the board, so it can never be imported by the Python installed on your computer.
This error means Thonny (or your editor) ran the script on the PC. The fix is not in your code — point the editor at the board, then run the same file again.
Quick fix — try in order
- 1
Switch Thonny to the board
Click the interpreter name in the bottom-right corner of Thonny and choose MicroPython (Raspberry Pi Pico) or MicroPython (ESP32). The Shell should then show a
MicroPython v1.xbanner. - 2
Or set it in Run → Configure interpreter
Pick the MicroPython variant and the board’s COM port (or “Try to detect port automatically”).
- 3
Do not pip install machine
The
machinepackage on PyPI is unrelated. Installing it gives new, stranger errors.machineonly exists inside MicroPython firmware. - 4
VS Code / PyCharm users
Pressing the normal Run button uses the PC’s Python. Run the file on the board with
mpremote run blink.pyinstead.
Why it happens
How do I know which Python ran my code?
The error wording tells you. CPython on a PC says ModuleNotFoundError: No module named 'machine'; MicroPython says ImportError: no module named …. The Windows path in the traceback is another giveaway.
The board is not in the interpreter list
MicroPython is not flashed yet, or the USB driver/port is missing. See Thonny couldn’t find the device.
It worked yesterday
Thonny remembers the last interpreter. If the board was unplugged at start-up, it may have fallen back to Local Python 3.
Board-specific notes
Pico W: After picking the interpreter, Pin('LED') is the on-board LED. See Pico W pinout.
ESP32: Choose MicroPython (ESP32) — the generic “MicroPython (generic)” option also works but does not reset the board for you.
Did this fix it?
Thanks — that helps us keep this page right.