Import a module by a name stored in a variable? (importlib?)


  • Pybytes Beta

    I want to import a module name stored in a variable. This StackOverflow post says it it is possible in standard Python. How would I do the same in uPython?

    Here's an example:

    device_routines/
    ... __init__.py
    ... command.py
    
    $ cat command.py
    print("Inside command.py")
    
    >>> import importlib
    >>> mymodule = 'device_routines.command'
    >>> myimport = importlib.import_module(mymodule)
    Inside command.py
    >>>

Log in to reply
 

Pycom on Twitter