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: 60
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: 15
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: 2702
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: 15
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...

User avatar
Roberthh
Posts: 936
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: 15
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: 2702
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: 2702
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

Post Reply