Page 1 of 1

How to check if a python module exists without importing it

Posted: Sun Sep 16, 2018 12:34 am
by HermannSW
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?

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

Posted: Sun Sep 16, 2018 1:26 am
by jickster
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

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

Posted: Sun Sep 16, 2018 4:00 am
by deonis
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) 

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

Posted: Sun Sep 16, 2018 5:53 am
by pythoncoder
@deonis help('modules') only lists those modules which are frozen as bytecode.

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

Posted: Sun Sep 16, 2018 10:27 am
by HermannSW
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

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

Posted: Sun Sep 16, 2018 3:08 pm
by jickster
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

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

Posted: Sun Sep 16, 2018 5:11 pm
by jickster
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