[Python] Import class from module dynamically

Discussão em 'Python' iniciado por Stack, Setembro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have class called 'my_class' placed in 'my_module'. And I need to import this class. I tried to do it like this:

    import importlib
    result = importlib.import_module('my_module.my_class')


    but it says:

    ImportError: No module named 'my_module.my_class'; 'my_module' is not a package


    So. As I can see it works only for modules, but can't handle classes. How can I import a class from a module?

    Continue reading...

Compartilhe esta Página