Can't find pins class

Trying to read an analog voltage on either pin 0, 1, or 2. Can do this in MakeCode but want to do it in Python instead. When I do it in MakeCode, and then I switch the editor to get the equivalent Python code, that code looks like this:

def on_forever():
basic.show_number(pins.analog_read_pin(AnalogPin.P0))
basic.forever(on_forever)

When I paste that code into the Python editor, it can’t find ‘basic’ or ‘pins’. I’m assuming those are needed imports but I can’t figure out how to resolve that problem.

Thanks

Are you pasting it into the MakeCode Python editor or the Python-only editor, like this one: https://python.microbit.org/v/3 ?

The Python-only editor uses a different version of Python than MakeCode. You can read about the differences between the two types of Python here: https://support.microbit.org/support/solutions/articles/19000111744-makecode-python-and-micropython

The different version, called MicroPython, has different names for the commands, which are described here: https://microbit-micropython.readthedocs.io/en/v2-docs/. In the Python-only (MicroPython) version it would look like this:

from microbit import *

while True:
    display.show(pin0.read_analog())

Thanks I’ll try!

It’s really not clear which Python context I’m in. How does one determine this from the navigation / GUI?

David

Hi @bethesda,

The MakeCode Python code only works in MakeCode. If you are pasting the code into anyplace else, it’s probably expecting the MicroPython approach. That is, any place other than makecode.microbit.org is probably MicroPython and you need to use the approaches described here: https://microbit-micropython.readthedocs.io/ .

If you are using the MicroPython approach, the current micro:bit on-line Python editor works great! It’s at https://python.microbit.org/v/3 . Here’s a very brief overview (thanks to Kitronic): https://www.youtube.com/watch?v=6dLYpAume-A

Bill