How to obtain a list of importable modules?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
pagano.paganino
Posts: 65
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: How to obtain a list of importable modules?

Post by pagano.paganino » Thu Nov 09, 2017 2:56 pm

Hi Lisa,
i haven't esp32, on pyboard a trick can be:

Code: Select all

import io
import os

class DUP(object):

    def __init__(self, s):
        self.s = s

    def write(self, data):
        self.s += data
        return len(data)

    def readinto(self, data):
        return 0

s = bytearray()
os.dupterm(DUP(s))
help('modules')
os.dupterm(None)
into 's' you can see the output from help('modules')

LisaM
Posts: 18
Joined: Tue Nov 07, 2017 6:11 pm

Re: How to obtain a list of importable modules?

Post by LisaM » Thu Nov 09, 2017 3:10 pm

AttributeError: 'module' object has no attribute 'dupterm'

:(

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

Re: How to obtain a list of importable modules?

Post by pythoncoder » Thu Nov 09, 2017 6:17 pm

@pagano.paganino Ingenious!
@LisaM I think dupterm is yet to be implemented on ESP32.
Peter Hinch

LisaM
Posts: 18
Joined: Tue Nov 07, 2017 6:11 pm

Re: How to obtain a list of importable modules?

Post by LisaM » Fri Nov 10, 2017 12:29 am

Running on pyboard:

File "test.py", line 17, in <module>
ValueError: need a UART object

test.py:

Code: Select all

import io
import os

class DUP(object):

    def __init__(self, s):
        self.s = s

    def write(self, data):
        self.s += data
        return len(data)

    def readinto(self, data):
        return 0

s = bytearray()
os.dupterm(DUP(s))
help('modules')
os.dupterm(None)
print(s)
Line 17: os.dupterm(DUP(s))

So... not working on pyboard either...

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

Re: How to obtain a list of importable modules?

Post by Roberthh » Fri Nov 10, 2017 6:34 am

You might need to update. It works on my PyBoard:
(sysname='pyboard', nodename='pyboard', release='1.9.3', version='v1.9.3-11-gb9580b8-dirty on 2017-11-07', machine='PYBv1.1 with STM32F405RG')

LisaM
Posts: 18
Joined: Tue Nov 07, 2017 6:11 pm

Re: How to obtain a list of importable modules?

Post by LisaM » Fri Nov 10, 2017 11:02 am

MicroPython v1.9.2-173-g829c329-dirty on 2017-10-20; PYBv1.0 with STM32F405RG
Hmmm, little bit difference. I was going to update anyway, due to #3379. I'll test it after the update.

It also need to work on all of my testbeds, being : Orange PI Zero, ESP32, NodeMCU, Pyboard.
For now i solved it by using the __init__.py file (in the plugins dir) to write down which plugins are available in the (frozen!) plugins dir.

Thx!

Edit: Tested with new release and there it works!

upymar
Posts: 6
Joined: Sat Feb 04, 2017 7:47 am

Re: How to obtain a list of importable modules?

Post by upymar » Fri Jun 15, 2018 12:29 pm

How do I get a list of available functions of a specific module?

In another thread I read something about e.g. the ntptime module but didn't find anything in the documentation.

So where do I get the information how to use modules and their functions which are not mentioned in the documentation?

Thanks and best regards

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

Re: How to obtain a list of importable modules?

Post by pythoncoder » Sat Jun 16, 2018 6:52 am

ntptime is not included on the Pyboard by default as it has no network connection, but it can be found on the ESP8266.

Code: Select all

help('modules')
provides a list of compiled-in modules. This list does not include modules you might have put in the device's filesystem.

Code: Select all

import ntptime
dir(ntptime)
will list its methods. Or you can look at the code in the source tree.

Ideally all these modules would be documented but sometimes, unfortunately, you do have to track down the code and take a look.
Peter Hinch

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

DUP class above

Post by pythoncoder » Sun Jul 08, 2018 6:34 am

For anyone wanting to use the DUP class, in this forum thread @Damien said:
There was a recent change to how this stuff works behind the scenes and you now need to derive the DUP class from io.IOBase: class DUP(io.IOBase)
Peter Hinch

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

Re: How to obtain a list of importable modules?

Post by HermannSW » Sat Oct 20, 2018 1:02 pm

pythoncoder wrote:
Sat Jun 16, 2018 6:52 am

Code: Select all

help('modules')
provides a list of compiled-in modules. This list does not include modules you might have put in the device's filesystem.
Interesting, I compared my ESP01s

Code: Select all

MicroPython v1.9.4-272-g46091b8a on 2018-07-18; ESP module with ESP8266
and ESP32

Code: Select all

MicroPython v1.9.4-623-g34af10d2e on 2018-10-03; ESP32 module with ESP32
Micropython modules (by "$ cat | for w in `cat`; do echo $w; done | sort > esp__.txt" and copy&paste).
There are 3 modules that only exist on ESP8266, and 10 modules that only exists on ESP32:

Code: Select all

$ diff --side-by-side -W 35 esp8266.txt esp32.txt 
apa102	      |	apa106
array		array
binascii	binascii
_boot		_boot
btree		btree
builtins	builtins
	      >	cmath
collections	collections
dht		dht
ds18x20		ds18x20
errno		errno
esp		esp
	      >	esp32
flashbdev	flashbdev
framebuf	framebuf
gc		gc
hashlib		hashlib
	      >	heapq
inisetup	inisetup
io		io
json		json
lwip	      <
machine		machine
__main__	__main__
math		math
micropython	micropython
neopixel	neopixel
network		network
ntptime		ntptime
onewire		onewire
_onewire	_onewire
os		os
port_diag     <
random		random
re		re
select		select
socket		socket
ssl		ssl
struct		struct
sys		sys
	      >	_thread
time		time
ubinascii	ubinascii
ucollections	ucollections
ucryptolib	ucryptolib
uctypes		uctypes
uerrno		uerrno
uhashlib	uhashlib
	      >	uhashlib
uheapq		uheapq
uio		uio
ujson		ujson
	      >	umqtt/robust
	      >	umqtt/simple
uos		uos
upip		upip
upip_utarfile	upip_utarfile
	      >	upysh
urandom		urandom
ure		ure
	      >	urequests
uselect		uselect
usocket		usocket
ussl		ussl
ustruct		ustruct
utime		utime
utimeq		utimeq
uzlib		uzlib
webrepl		webrepl
_webrepl	_webrepl
webrepl_setup	webrepl_setup
websocket	websocket
websocket_hel	websocket_hel
zlib		zlib
$ 
I went a step further and wanted to compare the common module features between both MicroPythons:

Code: Select all

$ comm -12 esp8266.txt esp32.txt > common.mod
$ 
With webrepl_client.py "-r" option and input redirect this was easy to do (I did reconnect to esp8266 Wlan after the first command was executed while connected to ESP32 Wlan):

Code: Select all

$ webrepl_client.py -p abcd -r 192.168.4.1 < <(sleep 1 && for m in `cat common.mod`;  do echo -e "help(\"$m\")"; done; echo "exit") > esp32.detail
$ webrepl_client.py -p abcd -r 192.168.4.1 < <(sleep 1 && for m in `cat common.mod`;  do echo -e "help(\"$m\")"; done; echo "exit") > esp8266.detail
$ 
First I converted to Unix format. The diffs.txt file is attached:

Code: Select all

$ dos2unix esp32.detail esp8266.detail 
dos2unix: converting file esp32.detail to Unix format ...
dos2unix: converting file esp8266.detail to Unix format ...
$ 
$ diff --side-by-side -W 60 esp8266.detail esp32.detail > diffs.txt
There is no feature that does exist on esp8266 Micorpython only, but there are 248 in the 62 common modules that exist on esp32 only:

Code: Select all

$ diff --side-by-side esp8266.detail esp32.detail | grep "  <"
$ diff --side-by-side esp8266.detail esp32.detail | grep "  >" | wc --lines
248
$ 
But there are only 4 different features:

Code: Select all

$ grep "  >" diffs.txt  | sort -u
			     >	  center -- <function>
			     >	  partition -- <function>
			     >	  rpartition -- <function>
			     >	  splitlines -- <function>
$ 

P.S:
Ooops -- I will redo the evaluation, eg. for module utime I did

Code: Select all

help('utime')
which is wrong, instead this is needed:

Code: Select all

import utime
help(utime)
Attachments
diffs.zip
contains diffs.txt
(2.03 KiB) Downloaded 6 times

Post Reply