hippy
Posts: 2459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Installing MicroPython - How To Guide

Fri Aug 25, 2017 9:21 pm

Updated : 27 Dec 2017

The following instructions detail how to install MicroPython on a Pi. Tested and working with Raspbian Stretch on a Pi Zero W and Pi 3B so should work on any Pi variant.

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 should already be installed by default with Raspbian but the 'libffi-dev' is likely not -

Code: Select all

sudo apt-get install git
sudo apt-get install build-essential
sudo apt-get install libffi-dev
2) Getting the MicroPython source

Code: Select all

cd ~
git clone https://github.com/micropython/micropython.git
This will have created a 'micropython' sub-directory in your 'pi' home directory.

3) Building MicroPython for the Pi

Code: Select all

cd micropython
cd ports                                   -- See notes below
cd unix
make clean
make axtls
make
Note that 'make axtls' and 'make' may generate warnings which can (probably) be ignored. The 'make axtls' takes about 5 minutes on a Pi Zero W, 'make' less than 10 minutes.

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.

4) Testing the MicroPython build

Run the MicroPython executable with -

Code: Select all

./micropython
This should show something like -

Code: Select all

MicroPython v1.9.3-217-g5de064fb on 2017-12-26; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>>
Enter the following a line at a time, pressing return at the end of each line to execute that line -

Code: Select all

print("Hello World!")
import uos
print(dir(uos))
Use Ctrl-D to exit to the Raspbian command prompt.

5) Installing micropython-lib packages

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>
Where <micropython-package> should be replaced by the name of the package to install.

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
Note that if the 'micropython-' prefix is not specified for the package name then 'upip' may install an incorrect or earlier version of the desired package.

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))
Edit History

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

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
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
Last edited by hippy on Wed Dec 27, 2017 3:18 pm, edited 7 times in total.

BoKKeR
Posts: 6
Joined: Wed Nov 20, 2013 9:14 pm

Re: Installing MicroPython - How To Guide

Sun Dec 24, 2017 8:10 pm

would this allow me to develop scripts that I can later move to a esp8266 for example? is there some limitations like i2c not working etc? if not this would make me really happy

hippy
Posts: 2459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Installing MicroPython - How To Guide

Mon Dec 25, 2017 12:38 pm

BoKKeR wrote:
Sun Dec 24, 2017 8:10 pm
would this allow me to develop scripts that I can later move to a esp8266 for example? is there some limitations like i2c not working etc? if not this would make me really happy
The instructions above create the generic 'unix' version of MicroPython on a Pi. Physical hardware interfacing would done be through imported modules. You might have to create those yourself; those can be real modules which work or skeleton modules which just provide for testing code.

The easiest way to find out is probably to try it.

Glasairman
Posts: 12
Joined: Fri Mar 16, 2012 9:55 am
Location: Munich, Germany

Re: Installing MicroPython - How To Guide

Tue Dec 26, 2017 11:30 am

My install on a Pi Zero failed at the step cd unix as the subfolder of ~/micropython was not created during the installation as expected and therefore make clean was not possible, even though I created the folder manually. Any tips from anyone?

3) Building MicroPython for the Pi

Code: Select all

cd micropython
cd unix
make clean
make axtls
make

hippy
Posts: 2459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Installing MicroPython - How To Guide

Tue Dec 26, 2017 12:11 pm

Glasairman wrote:
Tue Dec 26, 2017 11:30 am
My install on a Pi Zero failed at the step cd unix as the subfolder of ~/micropython was not created during the installation as expected
Looking at https://github.com/micropython/micropython/releases it appears they have recently moved the various ports to a "ports" directory. Try a cd ports before a cd unix.

I am not sure in which directory the MicroPython executable will get placed in. When I have a Pi available I will set about getting and testing the latest version and updating the earlier instructions.

hippy
Posts: 2459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Installing MicroPython - How To Guide

Tue Dec 26, 2017 1:03 pm

hippy wrote:
Tue Dec 26, 2017 12:11 pm
Try a cd ports before a cd unix.
That appears to be the solution. I have updated the instructions in the first post.

When I tried to compile after updating my original cloned version I got a compilation error. Deleting the entire '~/micropython' directory and sub-directories, following the updated installation and build instructions worked for me on a Pi 3B.

BoKKeR
Posts: 6
Joined: Wed Nov 20, 2013 9:14 pm

Re: Installing MicroPython - How To Guide

Wed Dec 27, 2017 1:08 pm

I got it to work but with upip all the packages seem outdated. I looked at ureqests and its on 1.2 instead of 5.2 as opposed to the esp8266 urequest original build. is this something that can be copied over or are the packages developed separately

hippy
Posts: 2459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Installing MicroPython - How To Guide

Wed Dec 27, 2017 2:44 pm

BoKKeR wrote:
Wed Dec 27, 2017 1:08 pm
I got it to work but with upip all the packages seem outdated. I looked at ureqests and its on 1.2 instead of 5.2 as opposed to the esp8266 urequest original build. is this something that can be copied over or are the packages developed separately.
The issue may be how you are installing packages.

With 'install urequests' that appears to get version 0.1.2 -

Code: Select all

pi@Pi3B:~/micropython/ports/unix$ ./micropython -m upip install urequests
Installing to: /home/pi/.micropython/lib/
Warning: pypi.python.org SSL certificate is not validated
Installing urequests 0.1.2 from https://pypi.python.org/packages/3d/c3/211eeedd2bf6b7be8781b2ad9153f18b848ad50612b778bcab59d30d4a63/urequests-0.1.2.tar.gz
With 'install micropython-urequests' that appears to get version 0.5.1 -

Code: Select all

pi@Pi3B:~/micropython/ports/unix$ ./micropython -m upip install micropython-urequests
Installing to: /home/pi/.micropython/lib/
Warning: pypi.python.org SSL certificate is not validated
Installing micropython-urequests 0.5.1 from https://pypi.python.org/packages/a6/8a/d6656982387259da74965a04ae55bde4651a64aa1772cee91bed0a3eebab/micropython-urequests-0.5.1.tar.gz
That 0.5.1 seems to be the latest version on PyPi ...

https://pypi.python.org/pypi?:action=se ... icropython
https://pypi.python.org/pypi/micropython-urequests

Members of the MicroPython community may be able to provide additional help or guidance. I'll add a link to the MicroPython Forum in the initial post -

https://forum.micropython.org

BoKKeR
Posts: 6
Joined: Wed Nov 20, 2013 9:14 pm

Re: Installing MicroPython - How To Guide

Wed Dec 27, 2017 10:28 pm

Thanks that worked great.

Return to “Python”

Who is online

Users browsing this forum: Google [Bot] and 33 guests