This page (revision-21) was last changed on 2025-07-30 13:27 by Murray Altheim

This page was created on 2025-07-30 02:24 by Murray Altheim

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
21 2025-07-30 13:27 826 bytes Murray Altheim to previous

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 3 added one line
%%info
At line 4 removed 9 lines
The file {{main.py}} is special in MicroPython, only in the sense that when you power-on the device and/or press the reset button, the main.py file is executed.
%%filename main.py %%
%%python
\\# this is a comment
print('hello world.')
print("hello again.")
At line 15 removed one line
In Python there is a {{try-except-finally}} structure called a "compound statement" consisting of three separate __blocks__, which is ideal for a main.py file.
At line 17 changed one line
In the below example, there is a {{try}} block, followed by an {{except}} block, followed by a {{finally}} block. A {{try}} block must be followed by an {{except}} block and/or a {{finally}} block (you must have one of them).
The file (also sometimes called a "script") named {{main.py}} is special in MicroPython, in the sense that when you power-on the device and/or press the reset button, the contents of the {{main.py}} file are passed to the MicroPython interpreter and the program contained within main.py is executed. It's the ''entry point'' for running any application in Python.
At line 20 changed 2 lines
%%python
try:
%%python \\# this is a comment
At line 23 changed 8 lines
print('hello world.') # remember to indent the insides of your blocks by 4 characters!
except Exception:
# this block is executed if there is an exception (error)
print('an error occurred.')
finally:
# this is always executed, no matter what
print('complete.')
print('hello world.')
print("hello again.") # you can use single or double quotes
At line 33 removed one line
If you want to use the exception to print out an error message you need to assign it to a variable (in this case, we've used {{ex}}).
At line 35 changed 3 lines
%%filename main.py %%
%%python
try:
__See also:__ [The REPL|MicroPythonTutorialTheREPL]
At line 39 removed 9 lines
print('hello world.')
except Exception as ex:
print('an error occurred: {}'.format(ex))
finally:
print('complete.')
%%