This article contains a guide to the micro:bit Python Editor v3.

Python Editor Overview

Here is an overview of the micro:bit Python Editor:

The micro:bit Python Editor has been designed particularly with beginners to text-based coding in mind. The bright, colourful interface and range of features aim to lower the barrier to getting started and are designed to appeal to 

a diverse range of students, helping more young people towards their best digital future.


The editor is divided into three panels:

  1. Left sidebar menu: by default this shows the Reference section. Use the buttons on the left-hand side to switch between the Reference, Ideas, API, Project, Settings and Help menus. 
  2. Edit window: this is your Python code. Tap in here and type to make changes or drag and drop code from the left sidebar menu into this area.
  3. Simulator: test out your Python code before sending it to a micro:bit using the simulator. Tap the play symbol to run the current code and use the controls to simulate different conditions.


How to use the Editor


Editing the code

This video summarises some basics on how to edit your code using typing and auto-complete: 



You can make edits to your program in the edit window in the middle of the screen. For example, if you want to change the image from the default HEART to HAPPY, you can delete the word for the image and replace it. The auto-complete feature suggests different images that you can choose from the drop-down list. If you make a mistake, you can use the undo or redo buttons to correct it.


The image above highlights how to:

  • Get more space to edit: Minimise the left menu (1), right menu (3) or adjust their width (2).
  • Zoom in and out: using the controls at the top right (4)
  • Undo/redo edits: using the controls also at the top right (5)


The image below highlights other key editing features.




Using the simulator to test your code

The micro:bit in the sidebar on the right is a simulator. Press play to try out your code. If your code uses functionality such as the accelerometer, temperature sensor, light sensor (or any other micro:bit function) then you can simulate the conditions you need to test your micro:bit with the controls beneath. You can also use the simulated serial console to interact with the simulated micro:bit too. 




Sending your program to your micro:bit

When you are ready to try your programme on your micro:bit choose 'Send to micro:bit'. 


The following video shows the next steps if your browser supports WebUSB (e.g. Chrome and Edge browsers) as webUSB is used by default: 



Check your micro:bit is attached via a USB cable to your computer then follow the steps to connect and send your programme (otherwise known as 'flashing').


If your browser does not support webUSB, this video shows the alternative route to send your program to your micro:bit:

Choose 'Save' to save your programme to the downloads folder on your local computer. Connect your micro:bit to your computer by USB and you will see it appear like a drive in your file system. Find the saved programme (i.e. in Downloads), then drag and drop it onto the MICROBIT drive to send/flash it to the micro:bit.


You can save your program as a .hex file or a .py script at any time using the save button to keep it for later. 

Opening a program


To open a program (e.g. one you saved earlier or one provided by a teacher or third party), choose the 'Open' button. 

From here, select the .hex or .py file that you wish to open and then click open. Alternatively, you can drag and drop a .hex file or .py script into the editor.



Using the Reference menu


The reference menu contains working examples of code that you can use in your project. The examples relate to micro:bit features and Python concepts. Here we are using an example for the micro:bit buttons.


  1. Navigate to the Buttons item in the Reference menu. 
  2. Drag and drop the example code to the editor or tap on it and select Copy code and then paste it. You can put this anywhere in the program. In this example, we are adding the if statement to the existing while True loop so it merges the two while True loops into one otherwise the second while True would be unreachable.

Using the Ideas menu


The ideas menu contains complete Python projects to try out on the micro:bit. To use them, replace the code in the edit window with the code from the idea. You can then edit the code to make it your own.

Using the API menu


The API (Application Programming Interface) menu provides a more detailed overview of the MicroPython commands that you can use with the micro:bit. This can be useful when you want to understand more about what a particular piece of code is doing and what options or arguments you can use with a function.




You can access it from the sidebar menu or by using the autocomplete feature, which offers you a range of options via  a dropdown menu. You can click on 'API' to be taken to the API entry for the option you are looking at.

The documentation is also published in full at https://microbit-micropython.readthedocs.io


Using the serial console

To use the serial console you need to have a micro:bit device plugged into your computer, and use a browser that supports WebUSB, currently Chrome or Edge. Connect and flash your micro:bit by clicking the “Send to micro:bit” button. Once this has been completed, you’ll see a black bar appear at the bottom of the editor. Click “Show serial” to access the serial console.

The serial console allows you to inspect text output by your program via the print() function. This is particularly useful for debugging or to review how the state in your program is changing over time. You are also able to input text into your program where the input() function is used.>

See the Text Input and Output section within Reference inside the Python editor for more details on how to use this function. 


You can also interrupt and restart your program using Ctrl+C and Ctrl+D, respectively. When in an interrupted state, you are able to type and execute Python commands directly on your micro:bit which is a great way to try out commands with instant feedback. You can also interact with variables in your program and inspect how they have changed from their initial state when the program was started.

The serial console also shows the full output of runtime errors, which can be helpful when debugging.


For shortcuts that can be used within the serial console see: Python Editor: shortcuts.


Errors

See our guide to troubleshooting errors in the micro:bit Python Editor for more information.


As with any written language, you occasionally run into errors that you will need to fix in order for your program to run.


The Python Editor shows you the errors in your code, so you know what you need to fix before sending (flashing) your code to the micro:bit.


Errors are indicated in the margin by a red dot or grey square. They are also indicated  by using  a red wavy underline:

 
Setting the language



Click on the setting cog in the bottom left of the screen, choose languages and then choose the language from the menu. 


Using the projects menu

The projects section, found in the left hand menu, is particularly useful if you need to add additional Python files to your project. This may be, for example, to add a module for an accessory or to add code provided by a teacher.


Using accessories

If you want to use accessories alongside your micro:bit and program them with Python then your project will need to include a module (an additional .py file) that provides the code to run that accessory and you will need to import this module into your main program. 

Refer to the documentation provided by the accessory provider to locate the Python module for the accessory you are using and follow the instructions provided. The module may come in a number of forms: 

  • Zip files: if a zip file is provided you will first need to extract the contents of the zip file to a folder of your choice. Once extracted, inside the zip file you should find a module in the form of a .py file or a .hex file.
  • .PY files: If the accessory provider has provided the module as a .py file, watch the video in the 'Using the projects menu' section above to understand how to add this module as an additional file in your project. Once added, go back to main.py and add code to import the functions within that module so that you can use them in your project. The line of code you need to add will be something like: module_name import *
  • .HEX files: If the accessory provider has provided a .hex file, use the main 'Open' button in the Python Editor to open this file. In most cases, this will open with the module already included in the project and the code to import the module already included at the top of main.py.


By default, you will not be able to edit third-party modules provided by accessories providers (to avoid accidentally adding bugs) but if you are an advanced user who would like to edit the module then visit settings to enable this. 


If you have any issues getting the micro:bit Python Editor to work with an accessory it is typically best to check documentation and support provided by the accessory provider. 


Useful shortcuts

A list of shortcuts available within the Python Editor to support accessibility and productivity are list in this article: https://support.microbit.org/a/solutions/articles/19000139603


Troubleshooting, Further help and FAQ

Can I use my Python Editor code in MakeCode?

Not at the moment. The MakeCode editor uses its own version of Python. You can read more about this in our article MakeCode Python and MicroPython


How do I save projects to return to edit late?

Choosing 'Save' will save your hex.file to your downloads folder by default, you can move it to another folder if you wish (e.g. to their school cloud storage). Use the 3 dots menu next to the 'Save' button to download a Python Script (.py file). Just use the 'Open' button to return to the project later. 


Can I use the Python Editor with both micro:bit V1 and V2?

The micro:bit Python Editor works with both the micro:bit V1 and V2 with built-in speaker. The simulator shows a micro:bit V2, so if you are using a micro:bit V1 do be aware that some code may work on the simulator but not on your physical micro:bit. Code that will only work on a micro:bit V2 is labelled 'V2' in the Reference section. 


Does this editor work with Mu?

Yes, you can use your MicroPython scripts or .hex files from Mu in the micro:bit Python Editor

Can I use the micro:bit Python Editor offline? 

You need an internet connection and web browser to load the editor. Once loaded it will continue to work, even if your internet connection becomes unstable.


Where can I find more information on teaching Python with the micro:bit?
You can find a range of projects and lessons on the microbit.org website


Can I use this editor to teach standard Python concepts?

Yes. We've put a serial console into the editor so you can do more traditional Python coding using your keyboard input and printing to the screen. Your BBC micro:bit becomes a real, tiny physical Python computer you can program and hold in your hand.


You can also use the serial console to read any Python error messages that scroll across the micro:bit's display, making it easier to debug 'runtime' errors.


Does the editor work with micro:bit classroom?

Yes, you can choose to use the Python Editor when you set up a classroom session.


Can I use accessories within the new Python Editor? 

Yes, if the accessory manufacturer has provided a Python. module as a .py file you can open this as a new file in the Projects section (be sure to use the cog on the right to open it as a new file rather than to replace your main file). You can then import the functions from the module into your main code. 


Can I still use the old editor? 

Yes, you can still access the old micro:bit Python Editor at https://python.microbit.org/v/2. It will continue to be available at least until September 2023. 


Where do I report bugs or request features?
You can report bugs to the foundation via creating a ticket here on support.microbit.org or if you are comfortable using Github via https://github.com/microbit-foundation/python-editor-next/issues