Page 1 of 3

How to obtain a list of importable modules?

Posted: Sat Apr 22, 2017 9:57 am
by evbaarle
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.

Re: How to obtain a list of importable modules?

Posted: Sat Apr 22, 2017 7:32 pm
by dhylands
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.

Re: How to obtain a list of importable modules?

Posted: Sun Apr 23, 2017 6:40 am
by evbaarle
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>
>>>

Re: How to obtain a list of importable modules?

Posted: Sun Apr 23, 2017 10:06 am
by pythoncoder
You need to upgrade the firmware.

Re: How to obtain a list of importable modules?

Posted: Sun Apr 23, 2017 6:00 pm
by dhylands
Yeah it looks like the help('modules') was added on Jan 21 and your build is from Dec 31.

Re: How to obtain a list of importable modules?

Posted: Tue Nov 07, 2017 6:16 pm
by LisaM
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.

Re: How to obtain a list of importable modules?

Posted: Tue Nov 07, 2017 7:17 pm
by dhylands
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.

Re: How to obtain a list of importable modules?

Posted: Tue Nov 07, 2017 7:57 pm
by LisaM
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').

Re: How to obtain a list of importable modules?

Posted: Wed Nov 08, 2017 7:26 am
by pythoncoder
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.

Re: How to obtain a list of importable modules?

Posted: Wed Nov 08, 2017 6:05 pm
by LisaM
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)?