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.
evbaarle
Posts: 6
Joined: Mon Mar 13, 2017 6:43 am

How to obtain a list of importable modules?

Post by evbaarle » Sat Apr 22, 2017 9:57 am

I have not been able to find a micropython command to get a list of all the modules that are included in the micropython firmware running on my board.
Brute force trying might be an option, or parsing the build configuration parameters, but I assume a more elegant method is available.

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

Re: How to obtain a list of importable modules?

Post by dhylands » Sat Apr 22, 2017 7:32 pm

Try:

Code: Select all

>>> help('modules')
__main__          lcd160cr          socket            umachine
array             lcd160cr_test     stm               uos
binascii          lib/hello         struct            urandom
builtins          machine           sys               ure
cmath             main              time              uselect
collections       math              ubinascii         usocket
errno             micropython       ucollections      ustruct
framebuf          network           uctypes           utime
gc                os                uerrno            utimeq
hashlib           pyb               uhashlib          uzlib
heapq             random            uheapq            zlib
io                re                uio
json              select            ujson
Plus any modules on the filesystem
This is the output on my pyboard.

evbaarle
Posts: 6
Joined: Mon Mar 13, 2017 6:43 am

Re: How to obtain a list of importable modules?

Post by evbaarle » Sun Apr 23, 2017 6:40 am

Hi Dave,
Just what I was looking for, but unfortunately my board and the online board at http://micropython.org/live/
both give this reply, whatever string literal i give as argument, maybe because I have the lite version of the pyboard or is this a bug?

MicroPython v1.8.6-273-g5efd650 on 2016-12-31; PYBLITEv1.0 with STM32F411RE
>>> help('modules')
object modules is of type str
encode -- <function>
find -- <function>
rfind -- <function>
index -- <function>
rindex -- <function>
join -- <function>
split -- <function>
splitlines -- <function>
rsplit -- <function>
startswith -- <function>
endswith -- <function>
strip -- <function>
lstrip -- <function>
rstrip -- <function>
format -- <function>
replace -- <function>
count -- <function>
partition -- <function>
rpartition -- <function>
center -- <function>
lower -- <function>
upper -- <function>
isspace -- <function>
isalpha -- <function>
isdigit -- <function>
isupper -- <function>
islower -- <function>
>>>

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

Re: How to obtain a list of importable modules?

Post by pythoncoder » Sun Apr 23, 2017 10:06 am

You need to upgrade the firmware.
Peter Hinch

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

Re: How to obtain a list of importable modules?

Post by dhylands » Sun Apr 23, 2017 6:00 pm

Yeah it looks like the help('modules') was added on Jan 21 and your build is from Dec 31.

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

Re: How to obtain a list of importable modules?

Post by LisaM » Tue Nov 07, 2017 6:16 pm

But... How do i get the output of the help("modules") into a string? I want to have a list of all frozen modules, so that you can delete plugins (xxx.py files in the subdirectory plugins) without having to change the source code. uos.listdir() doesn't work since they are frozen modules and not files. With the string i can parse the name of the frozen module out and dynamically load that frozen module.

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

Re: How to obtain a list of importable modules?

Post by dhylands » Tue Nov 07, 2017 7:17 pm

I guess I'm a bit confused. You can only delete files. If a module is frozen, then there is nothing you can change about it without rebuilding the firmware.

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

Re: How to obtain a list of importable modules?

Post by LisaM » Tue Nov 07, 2017 7:57 pm

Put 100 (plugin) .py files in the plugins directory, let user/programmer delete the ones they don't need to keep the firmware small and rebuild the firmware. Without changing code, no import changes, the program needs to find out which plugins remained, are now frozen and need to be imported using __import__.

By the way, when i have a file called gpio.py (string in var modnam) in the plugins directory then __import__(modname,globals(), locals(), [modname], 1) doesn't work (ImportError: no module named 'upyeasy.gpio').

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

Re: How to obtain a list of importable modules?

Post by pythoncoder » Wed Nov 08, 2017 7:26 am

I don't think there is a way programmatically to determine a list of frozen modules. So I would do this on the PC side.

Write a build script which lists the files in the plugins directory to a file, which is itself located in the frozen modules directory. Having done this the script builds the firmware. Note that the file must be written with Python syntax and have a .py extension, but it could simply declare a global list of filenames.

The MicroPython code imports the file and then imports each entry in the list.
Peter Hinch

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

Re: How to obtain a list of importable modules?

Post by LisaM » Wed Nov 08, 2017 6:05 pm

But... Help("modules") does show a list of frozen modules so the info is available in micropython (at least on esp32). Is there a way of redirecting the help output into a string for parsing (again on a esp32)?

Post Reply