How to check if a python module exists without importing it

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

How to check if a python module exists without importing it

Post by HermannSW » Sun Sep 16, 2018 12:34 am

This thread is very informative for different versions of Python:
https://stackoverflow.com/questions/140 ... porting-it

But the answers don't help for MicroPython, since neither v2 "imp" module nor v3 "importlib" module can be imported on MicroPython 1.9.4. What is the answer to the question for MicroPython?
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How to check if a python module exists without importing it

Post by jickster » Sun Sep 16, 2018 1:26 am

HermannSW wrote:This thread is very informative for different versions of Python:
https://stackoverflow.com/questions/140 ... porting-it

But the answers don't help for MicroPython, since neither v2 "imp" module nor v3 "importlib" module can be imported on MicroPython 1.9.4. What is the answer to the question for MicroPython?

The answer is it doesn’t exist.

Create an issue on github to create it.


Sent from my iPhone using Tapatalk Pro

deonis
Posts: 11
Joined: Wed Aug 08, 2018 3:02 am

Re: How to check if a python module exists without importing it

Post by deonis » Sun Sep 16, 2018 4:00 am

You can use

Code: Select all

help("modules")
or if you want more details for a specific module then you need to import and use simialr command to get details.
For example:

Code: Select all

import sys
help(sys) 

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

Re: How to check if a python module exists without importing it

Post by pythoncoder » Sun Sep 16, 2018 5:53 am

@deonis help('modules') only lists those modules which are frozen as bytecode.
Peter Hinch
Index to my micropython libraries.

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

Re: How to check if a python module exists without importing it

Post by HermannSW » Sun Sep 16, 2018 10:27 am

jickster wrote:
Sun Sep 16, 2018 1:26 am
The answer is it doesn’t exist.

Create an issue on github to create it.
Thanks, done:
https://github.com/micropython/micropython/issues/4148
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How to check if a python module exists without importing it

Post by jickster » Sun Sep 16, 2018 3:08 pm

deonis wrote:You can use

Code: Select all

help("modules")
or if you want more details for a specific module then you need to import and use simialr command to get details.
For example:

Code: Select all

import sys
help(sys) 
import-ing can have side-effects.

But yeah I was fundamentally wrong.


Sent from my iPhone using Tapatalk Pro

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How to check if a python module exists without importing it

Post by jickster » Sun Sep 16, 2018 5:11 pm

HermannSW wrote:This thread is very informative for different versions of Python:
https://stackoverflow.com/questions/140 ... porting-it

But the answers don't help for MicroPython, since neither v2 "imp" module nor v3 "importlib" module can be imported on MicroPython 1.9.4. What is the answer to the question for MicroPython?
Help(“modules”) shows: builtin, frozen string, frozen mpy.

It doesn’t show file-based .py or .mpy

Code: Select all

 mp_help_add_from_map(list, &mp_builtin_module_map);

    #if MICROPY_MODULE_WEAK_LINKS
    mp_help_add_from_map(list, &mp_builtin_module_weak_links_map);
    #endif

    #if MICROPY_MODULE_FROZEN_STR
    extern const char mp_frozen_str_names[];
    mp_help_add_from_names(list, mp_frozen_str_names);
    #endif

    #if MICROPY_MODULE_FROZEN_MPY
    extern const char mp_frozen_mpy_names[];
    mp_help_add_from_names(list, mp_frozen_mpy_names);
    #endif

Sent from my iPhone using Tapatalk Pro

Post Reply