Main image of article Exploring the Benefits of MicroPython
Although Python is a very useful general-purpose programming language, there are some areas where it wouldn't be your first choice. For instance, if you want to develop complex games, why wouldn't you consider using C++ first? And in the embedded-software world, C dominates. MicroPython is aimed at the embedded market; it is suitable for micro-controllers and small, embedded systems. As such, it's a pretty good implementation of Python 3.4, not totally complete but very usable. It's licensed under the liberal MIT license, so you can use it as you wish. Let’s explore it a bit.

Getting Started

I don't have any microcontroller boards, so I was more interested in trying out MicroPython on Ubuntu 17. As the implementation is still in beta, the best way to start is to follow the Wiki instructions. This means installing four libraries, cloning the repository, and then running make. (Time-wise, it means five to 10 minutes to fetch everything, and a couple more to build it.) After you've built it, make sure you run the tests with make test. When I did so, some 624 tests with 18,501 individual test cases were skipped; this is possibly due to my running Ubuntu under Hyper-V on my Windows box. You can see all of the Python source files for the tests in the tests folder; this, for example, is the integer divide-by-zero test:
    1 / 0
except ZeroDivisionError:

    0 ** -1
except ZeroDivisionError:

Big Differences

GitHub offers a page that lists all of the differences between MicroPython and Cpython 3, which is the standard Python 3 implementation. For example, it uses garbage counting, not reference counting, to manage memory; introspection/reflection are minimally supported. Many of these differences help keep the footprint small and make it suitable for embedding. MicroPython deliberately ships with a small subset of packages. However, many core Python libraries have been ported and are available in the GitHub MicroPython-lib. They are also available from PyPI (Python Package Index) with the prefix micropython-, and you can see them on PyPi. In addition, there's a utility similar to the Python pip, called upip, for installing MicroPython packages. It comes with the UNIX version; the command below fetches the pystone benchmark utility:
./micropython -m upip install micropython-pystone
I ran it from the REPL (interactive prompt) that you get when you run MicroPython:
MicroPython v1.9.1-193-g653a0c2d on 2017-07-26; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import pystone
>>> pystone.main()
Pystone(1.2) time for 50000 passes = 0.381
This machine benchmarks at 131234 pystones/second

Python vs. C

C was created as a portable assembly language and it's close to the metal. For Python to access hardware, there’s the provided stm module that lets you access and change registers and the GPIOA peripheral (on the pyboard—more on this later). As a language, C is quite bare-boned compared to Python, which provides dictionaries, lists, and other higher-level data structures. With C, you need to write your own, based on pointers, and that can prove quite delicate and prone to crashing (or you could get a third-party library). C is great for low-level programming but gets more complicated at higher levels, which is where Python becomes useful. MicroPython, which has a complete inline assembler if you need to drop down lower than C, adds still more functionality. (Don’t forget MicroPython is written in C and you can access C libraries from MicroPython.) There's no overall best language; each has strengths and weaknesses. I have used a screwdriver to hammer something and also removed screws with a hammer, and neither worked well! In light of that, let’s touch on one final aspect:


Along with the MicroPython language, there are development kits that support the pyboard standard. There's an ARM MicroPython pyboard, an AdaFruit, and others, costing anything from $13 to $130 (this summary page lists 28 different boards). The documentation discusses programming the board and provides a tutorial to get you started.


Why do I think MicroPython is worthy of your consideration? The Internet of Things is not going away, and Python enhances embedded development, making it quicker to bring products to market. I’m fairly certain there are many more Python developers out there than C, thanks to Python’s mainstream popularity (even if it can’t compete on execution speed with C programs). If you’re interested in building for the next generation of devices, this implementation is well worth checking out.