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

Installing MicroPython - How To Guide

Fri Aug 25, 2017 9:21 pm

Updated : 03 Oct 2018

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 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
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.

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
Then continue with the instructions below as if building for the first time.

3) Building MicroPython for the Pi

Code: Select all

cd ~
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.4-623-g34af10d2e-dirty on 2018-10-03; 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.

To run the MicorPython test suite just to confirm everything is how it should be -

Code: Select all

cd ~
cd micropython
cd tests
./run-tests
The tests should only take a couple of minutes to complete.

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))
6) Making MicroPython available everywhere

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/bin/micropython
This may not be the best or most appropriate way to do things but does generally work.

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/bin/micropython
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
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

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 Oct 03, 2018 9:45 am, edited 10 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: 3783
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: 3783
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: 3783
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: 3783
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

[email protected]:~/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

[email protected]:~/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.

HermannSW
Posts: 764
Joined: Fri Jul 22, 2016 9:09 pm

Re: Installing MicroPython - How To Guide

Wed Oct 03, 2018 1:11 am

Thanks, the instructions worked, and build times on Pi 3B+ are 0:57min/2:37min instead of 5min/10min.

I was happy with MicroPython on ESP32 and ESP01s sofar.
I did build on Raspberry in order to be able to run the test suite (in 1:23min):

Code: Select all

[email protected]:~/micropython/tests $ time ( ./run-tests )
...
pass  unicode/unicode_subscr.py
skip  unix/extra_coverage.py
pass  unix/ffi_callback.py
pass  unix/ffi_float.py
pass  unix/ffi_float2.py
660 tests performed (19011 individual testcases)
660 tests passed
62 tests skipped: builtin_help builtin_range_binop class_delattr_setattr io_buffered_writer namedtuple_asdict sys_getsizeof cmd_parsetree framebuf1 framebuf16 framebuf2 framebuf4 framebuf8 framebuf_subclass urandom_extra urandom_extra_float ure_groups ure_span ure_sub ure_sub_unmatched vfs_basic vfs_fat_fileio1 vfs_fat_fileio2 vfs_fat_more vfs_fat_oldproto vfs_fat_ramdisk vfs_userfs math_factorial_intbig mpy_invalid resource_stream native_closure native_const native_const_intbig native_misc native_try native_try_deep native_with schedule viper_addr viper_args viper_binop_arith viper_binop_comp viper_binop_comp_imm viper_binop_divmod viper_binop_multi_comp viper_cond viper_const viper_const_intbig viper_error viper_globals viper_import viper_misc viper_misc_intbig viper_ptr16_load viper_ptr16_store viper_ptr32_load viper_ptr32_store viper_ptr8_load viper_ptr8_store viper_subscr viper_try viper_with extra_coverage

real	1m23.021s
user	1m12.078s
sys	0m10.239s
[email protected]:~/micropython/tests $

Then I did run the tests against ESP32 module (that was what I really was interested in).
Slower, in 7:40min, and with 23 testcase failures:

Code: Select all

[email protected]:~/micropython/tests $ time ( ./run-tests --target esp32 --device /dev/ttyUSB0 --baudrate 115200 )
...
pass  misc/features.py
FAIL  misc/non_compliant.py
pass  misc/non_compliant_lexer.py
pass  misc/print_exception.py
pass  misc/rge_sm.py
skip  misc/sys_exc_info.py
588 tests performed (15933 individual testcases)
565 tests passed
66 tests skipped: builtin_range_binop class_delattr_setattr class_descriptor class_reverse_op exception_chain generator_name io_iobase namedtuple_asdict subclass_native_call sys_getsizeof machine_pinbase machine_pulse machine_signal ucryptolib_aes128_cbc ucryptolib_aes128_ecb ucryptolib_aes128_ecb_enc ucryptolib_aes128_ecb_inpl ucryptolib_aes128_ecb_into ucryptolib_aes256_cbc ucryptolib_aes256_ecb uhashlib_md5 ujson_dump_iobase ure_groups ure_span ure_sub ure_sub_unmatched vfs_userfs float2int_doubleprec_intbig float_divmod float_parse_doubleprec math_factorial_intbig heapalloc_bytesio2 meminfo memstats native_closure native_const native_const_intbig native_misc native_try native_try_deep native_with viper_addr viper_args viper_binop_arith viper_binop_comp viper_binop_comp_imm viper_binop_divmod viper_binop_multi_comp viper_cond viper_const viper_const_intbig viper_error viper_globals viper_import viper_misc viper_misc_intbig viper_ptr16_load viper_ptr16_store viper_ptr32_load viper_ptr32_store viper_ptr8_load viper_ptr8_store viper_subscr viper_try viper_with sys_exc_info
23 tests failed: async_with_break async_with_return builtin_round_int builtin_round_intbig builtin_type bytearray_construct class_staticclassmethod class_super gen_yield_from_executing gen_yield_from_throw2 generator_pep479 python36 try_finally_return3 ujson_dump ussl_basic vfs_fat_fileio1 builtin_float_abs float_parse math_domain_special math_fun_special python36 opt_level non_compliant

real	7m40.049s
user	1m55.984s
sys	0m13.652s
[email protected]:~/micropython/tests $

This is MicroPython version on ESP32:

Code: Select all

$ webrepl_client.py 192.168.4.1
Password: 

WebREPL connected
>>> 
>>> 
MicroPython v1.9.4 on 2018-05-11; ESP32 module with ESP32
Type "help()" for more information.
>>> 
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/fork-raspiraw      https://github.com/Hermann-SW/userland
https://github.com/Hermann-SW/wireless-control-Eachine-E52-drone      https://twitter.com/HermannSW

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

Re: Installing MicroPython - How To Guide

Wed Oct 03, 2018 9:59 am

HermannSW wrote:
Wed Oct 03, 2018 1:11 am
Then I did run the tests against ESP32 module (that was what I really was interested in).
Slower, in 7:40min, and with 23 testcase failures:
I am afraid I cannot really help with that as it appears one needs to have a PyBoard or something compatible to run the tests and I don't have any of those.

The best place to seek help on the ESP32 test issue might be from the MicroPython community - https://forum.micropython.org

Many thanks for the details on how to run the test suite. I have updated my original post to include that and how to keep the MicroPython source up to date.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 7 guests