How to obtain a list of importable modules?
How to obtain a list of importable modules?
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.
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?
Try: This is the output on my pyboard.
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
Re: How to obtain a list of importable modules?
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>
>>>
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>
>>>
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to obtain a list of importable modules?
You need to upgrade the firmware.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to obtain a list of importable modules?
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?
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?
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?
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').
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').
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to obtain a list of importable modules?
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.
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
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to obtain a list of importable modules?
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)?