The following instructions detail how to install MicroPython on a Pi. Previously tested and working with Raspbian Stretch on a Pi Zero W and Pi 3B so should work on any Pi variant. Latest successful build was tested with a Pi 3B running Buster on 7th July 2019.
Note that this is a version of MicroPython which runs similarly to how Python 2.7 and Python 3.x run on the Pi. It is an application which runs under Raspbian and is not a bootable MicroPython which replaces Raspbian or other OS.
MicroPython will not be of interest to most Pi users who should continue to use Python 2.7 or Python 3.x as usual. It can however be useful when making benchmark comparisons with other Python versions and is an ideal starting point for any Pi user interested in programming with MicroPython or developing it further, for the Pi or other platform.
1) Installing development tools
The first two may already be installed by default with Raspbian but the 'libffi-dev' will likely not be -
Code: Select all
sudo apt-get install git
sudo apt-get install build-essential
sudo apt-get install libffi-dev
Code: Select all
cd ~
git clone https://github.com/micropython/micropython.git
If you have previously installed MicroPython using 'git clone' you can bring it up to date using -
Code: Select all
cd ~
cd micropython
git pull
3) Building MicroPython for the Pi
Code: Select all
cd ~
cd micropython
cd ports See notes below
cd unix
make clean
make axtls
Code: Select all
cd ~
cd micropython
cd mpy-cross See notes below
make
Code: Select all
cd ~
cd micropython
cd ports
cd unix
make
Note the November 2017 release moved all ports of MicroPython into a 'ports' sub-directory. When MicroPython is built the executable will be placed in the '~/micropython/ports/unix' directory.
Note that latest releases require the 'mpy-cross' utility to be built before a 'make' will be successful. If getting a "../../mpy-cross/mpy-cross: Command not found" error 'mpy-cross' most likely needs building. This takes just a few minutes.
4) Testing the MicroPython build
Run the MicroPython executable with -
Code: Select all
./micropython
Code: Select all
MicroPython v1.11-126-g7c2e83324 on 2019-07-07; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>>
Code: Select all
print("Hello World!")
import uos
print(dir(uos))
To run the MicorPython test suite just to confirm everything is how it should be -
Code: Select all
cd ~
cd micropython
cd ports
cd unix
make test
This should also show the same results -
Code: Select all
cd ~
cd micropython
cd tests
./run-tests
MicroPython has a limited number of packages pre-installed with limited functionality. Most pre-installed package names are prefixed by a 'u', for example 'usocket' for MicroPython where it would normally be 'socket' for Python 2.7 and Python 3.x.
The micropython-lib project aims to provide more complete packages for MicroPython. A list of available micropython-lib packages is available at -
https://pypi.python.org/pypi?:action=se ... icropython
Note that not all packages are complete and not all functionality of the Python 2.7 or Python 3.x packages they aim to be equivalent to may be available.
Packages can be installed using -
Code: Select all
./micropython -m upip install <micropython-package>
For example to include the 'socket' package which is more comprehensive than the pre-installed 'usocket' package, use -
Code: Select all
./micropython -m upip install micropython-socket
Once a package has been installed it can then be specified using an 'import' as usual within your MicroPython program -
Code: Select all
./micropython
Code: Select all
import usocket
print(dir(usocket))
Code: Select all
import socket
print(dir(socket))
When MicroPython is built its executable will be created in the '~/micropython/ports/unix' directory. This means you need to be in that directory to run './micropython' or need to specify the executable's full path to run it from somewhere else. This is not always desirable nor convenient.
To make MicroPython available everywhere, and to allow it to be invoked using 'micropython', a symbolic link may be created as follows -
Code: Select all
sudo ln -s ~/micropython/ports/unix/micropython /usr/local/bin/micropython
This is a 'quick and dirty' way to make MicroPython available everywhere or have it temporarily available everywhere while investigating other ways to achieve the same which may better suit your own situation.
To remove the symbolic link if you created it and want that gone or you choose to make MicroPython available everywhere some other way -
Code: Select all
sudo rm /usr/local/bin/micropython
25 Aug 2017 - Original How To
26 Dec 2017 - Updated for 'ports' sub-directory
27 Dec 2017 - Clarified 'micropython-' prefix for package names
27 Dec 2017 - Added link for MicroPython Community Forum
17 Feb 2018 - Added making MicroPython available everywhere
03 Oct 2018 - Added how to keep MicroPython up to date
03 Oct 2018 - Added how to run the MicroPython test suite
03 May 2019 - Confirmed build instructions worked for me on a Pi 3B Stretch
05 May 2019 - Added link for MicroPython Bug Reports and Issues
07 Jul 2019 - Added building of mpy-cross step
07 Jul 2019 - Updated testing step
07 Jul 2019 - Updated symlink of MicroPython to be in /usr/local/bin
07 Jul 2019 - Confirmed build instructions worked for me on a Pi 3B Buster
Links - MicroPython
Home Page : https://micropython.org
Documentation : http://docs.micropython.org
Source Code : https://github.com/micropython/micropython
Release Notes : https://github.com/micropython/micropython/releases
Bug Reports : https://github.com/micropython/micropython/issues
Community Forum : https://forum.micropython.org
Links - MicroPython Packages (micropython-lib)
Packages List : https://pypi.python.org/pypi?:action=se ... icropython
Information : https://github.com/micropython/micropython-lib