Best way to "detect" MicroPython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
MrPaul
Posts: 2
Joined: Sun Jul 22, 2018 5:03 pm

Best way to "detect" MicroPython

Post by MrPaul » Sun Jul 22, 2018 5:11 pm

I'm writing a library which is targeted to be able to run under MicroPython, but also "regular" python and development/unit testing is largely happening under regular python.

There are a few things which need to have differences between them. Typical, of course, is the import; "ucollections" vs. "collections", etc. Since I'll likely be using CircuitPython these are slightly less bothersome, but I'd like to work with all of them. The obvious way is to try importing ucollections (for example) and on import error import collections, but that seems a bit clunky. Is there a better way?

const() is another thing to look out for; I'll need to define it for regular python. What is the most efficient way to detecting if it exists?

A few other places can get icky; for example, pySerial read() is readchar() in UART, but I guess isinstance or the like is a way to tackle those. Or could wrap UART to be minimally pySerial like (or vice versa of course), but would still need someway of telling which python.

I guess I can see solutions for most of these, but was looking for something like a "best practices" to write cross-platform apps.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Best way to "detect" MicroPython

Post by dhylands » Mon Jul 23, 2018 3:37 pm

I normally use os.uname().sysname. As far as I know all of the ports also have a builtin micropython module. so you could test if that succeeds or not.

I also typically create wrappers for things like serial ports, which allows me to emulate serial ports on various platforms and even use sockets instead of the serial port.

For example, see: https://github.com/dhylands/json-ipc The serial_port.py, socket_port.py, and stm_usb_port.py are all wrapper which provide the same (or similar) API.

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Best way to "detect" MicroPython

Post by HermannSW » Sun Sep 16, 2018 12:14 am

dhylands wrote:
Mon Jul 23, 2018 3:37 pm
I normally use os.uname().sysname.
That does return "esp8266" for my ESP-01s MicroPython.
Even uos.uname() does not show the string "MicroPython".
The version could be an indicator, though.
"MicroPython" gets shown when pressing CTRL-B.
Is there a programmatic way to query "MicroPython" string?

Code: Select all

$ ./webrepl_client.py -s
Password: 12345678

WebREPL connected
>>> os
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> uos.uname().sysname
'esp8266'
>>> uos.uname()
(sysname='esp8266', nodename='esp8266', release='2.0.0(5a875ba)', version='v1.9.4-481-g3cd2c281d on 2018-09-04', machine='ESP module with ESP8266')
>>> \x02

MicroPython v1.9.4-481-g3cd2c281d on 2018-09-04; ESP module with ESP8266
Type "help()" for more information.
>>> 
>>> 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Best way to "detect" MicroPython

Post by pythoncoder » Sun Sep 16, 2018 5:51 am

I do it the obvious way:

Code: Select all

try:
    from micropython import const
    upython = True
except ImportError:
    const = lambda x : x
    upython = False
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Best way to "detect" MicroPython

Post by Roberthh » Sun Sep 16, 2018 7:07 am

Many ways to rome. I use:

Code: Select all

import sys
if sys.implementation.name == "micropython":
	# whatever flag is set then

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Best way to "detect" MicroPython

Post by HermannSW » Sun Sep 16, 2018 10:15 am

Thanks, that is the easy solution I searched for:

Code: Select all

$ ./webrepl_client.py -s
Password: 12345678

WebREPL connected
>>> import sys
>>> sys.implementation
(name='micropython', version=(1, 9, 4))
>>> 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Best way to "detect" MicroPython

Post by HermannSW » Sun Sep 16, 2018 1:11 pm

Works for python3 (on Raspbian) as well (I know that MicroPython is derived from python3):

Code: Select all

$ echo -e "import sys\nprint(sys.implementation)" | python3 -
namespace(_multiarch='arm-linux-gnueabihf', cache_tag='cpython-35', hexversion=50660336, name='cpython', version=sys.version_info(major=3, minor=5, micro=3, releaselevel='final', serial=0))
$ 
Does not work for python2 though:

Code: Select all

$ echo -e "import sys\nprint(sys.implementation)" | python -
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'module' object has no attribute 'implementation'
$ 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

Post Reply