Python 3.x Как можно получить модуль по его названию?

Я пытаюсь написать модульного телеграм-юзербота, и дабы модульность была максимально удобной, я попытался сделать функцию/модуль который будет:

  1. Выводить список всех модулей;
  2. Выводить список импортированных модулей;
  3. Импортировать модули по названию (из строки);
  4. Перезагружать уже импортированные модули;

Но проблемы возникают на 4 пункте когда я передаю в rld() не модуль, а название модуля.

Главный вопрос: Как можно получить модуль по его названию?

Мой код:

from telethon import events
from variables import *
from importlib import import_module as imp
from importlib import reload as rld

@bot.on(events.NewMessage(pattern='\.mod', outgoing=True))
async def u_import(message):
    A = message.message.raw_text.split()
    output = "[More attr's]"
    f_key = A[1]
    s_key = A[2]
    try:
        if f_key == 'import' and s_key not in imported:
            imp(f'u_{s_key}')
            imported.append(s_key)
            output = f'[Success, imported {s_key}]'
        elif f_key == 'reload' and s_key in imported:
            rld(f'u_{s_key}')
            output = f'[Success, reloaded {s_key}]'
        elif f_key == 'list':
            if s_key == 'imported':
                output = f'[Imported:]\n{imported}'
            elif s_key == 'all':
                output = f'[Modules:]\n{modules}'
            else:
                output = '[Not a feature]'
        else:
            output = '[Not a feature]'
    except IndexError:
        output = "[More attr's]"
    await edit(message.message, text=output)

Ошибка которую я получаю:

File "/usr/lib/python3.6/importlib/__init__.py", line 139, in reload
    raise TypeError("reload() argument must be a module")
TypeError: reload() argument must be a module

Ответы (1 шт):

Автор решения: 4aYnOmAn

Простое сохранение только модулей в список мне не подошло и я просто сделал словарь из названий модулей и самих модулей.

from telethon import events
from variables import *
from importlib import import_module as imp
from importlib import reload as rld

@bot.on(events.NewMessage(pattern='\.mod', outgoing=True))
async def u_import(message):
    A = message.message.raw_text.split()
    output = "[More attr's]"
    f_key = A[1]
    s_key = A[2]
    try:
        if f_key == 'import' and s_key not in list(imported.keys()):
            mod = imp(f'u_{s_key}')
            imported.update({s_key: mod})
            output = f'[Success, imported {s_key}]'
        elif f_key == 'reload' and s_key in list(imported.keys()):
            mod = rld(imported[s_key])
            output = f'[Success, reloaded {s_key}]'
        elif f_key == 'list':
            if s_key == 'imported':
                output = f'[Imported:]\n{imported.keys()}'
            elif s_key == 'all':
                output = f'[Modules:]\n{modules}'
            else:
                output = '[Not a feature]'
        else:
            output = '[Not a feature]'
    except IndexError:
        output = "[More attr's]"
    await edit(message.message, output)
→ Ссылка