bbcmicrobit / micropython

Port of MicroPython for the BBC micro:bit
https://microbit-micropython.readthedocs.io
Other
603 stars 284 forks source link

Partially fixed kode example in 'How to use' #697

Closed Bakkom closed 3 years ago

Bakkom commented 3 years ago

A non-existing function named "microbit" was causing a NameError. Changed [Import microbit] to [from microbit import *]. NB! "dir(microbit)" is still making a NameError, since microbit isn't defined or a pre-existing function. I am not sure what this line is meant to do, so I've let it be.

microbit-carlos commented 3 years ago

What kind of problem did you get? Could you copy/paste the exact error message?

The code example here is correct, these things are more or less equivalent:

import microbit
microbit.display.show(microbit.Image.HAPPY)
from microbit import *
display.show(Image.HAPPY)
from microbit import display, Image
display.show(Image.HAPPY)

The main difference here is that the second option imports everything inside microbit into the current scope (so accelerometer, compass, etc are also available without doing something like microbit.compass) and the last option only imports display and Image.

Bakkom commented 3 years ago

If I run a microbit.example on a Micro:Bit it returns "NameError, Name "microbit" not defined" or something of the likes. This is also the case for "import microbit". "from microbit import example" is the only way I can import to the Micro:Bit

microbit-carlos commented 3 years ago

How did you flash MicroPython into the micro:bit board? import microbit should definitely work.

Bakkom commented 3 years ago

I don't know, someone else did it, but I'll flash it again then. I'm sorry. I was 100% sure the documentation was off since I'm running a python program on it flashed from the MicroPython IDE.

microbit-carlos commented 3 years ago

No worries, if you've encountered this issue it's likely somebody else might in the future as well, so we really appreciate reporting these things and any contributions are always greatly welcomed!

Were you using https://python.microbit.org or the Mu editor? or something else?

Bakkom commented 3 years ago

https://python.microbit.org What wasn't working was everything related to "microbit.example", but if I imported "from microbit import *", and dropped the "microbit."-prefix, everything was working beautifully.

microbit-carlos commented 3 years ago

Could you try again? This is what I currently get from the online Python Editor REPL after flashing a programme:

MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822
Type "help()" for more information.
>>> import microbit
>>> microbit.display.show(microbit.Image.HAPPY)
>>> 

And works as expected.

Bakkom commented 3 years ago

OK. I guess you are magic because now everything is working? Thank you.

microbit-carlos commented 3 years ago

🧙 In that case I'll close this PR. Thank you testing again and debugging through this.