How to find all child modules in Python?

后端 未结 6 1274
走了就别回头了
走了就别回头了 2021-02-19 11:46

While it is fairly trivial in Python to import a \"child\" module into another module and list its attributes, it becomes slightly more difficult when you want to import all

6条回答
  •  耶瑟儿~
    2021-02-19 12:24

    The solution above traversing the filesystem for finding submodules is ok as long as you implement every plugin as a filesystem based module.

    A more flexible way would be an explicit plugin list in your main module, and have every plugin (whether a module created by file, dynamically, or even instance of a class) adding itself to that list explicitly. Maybe via a registerPlugin function.

    Remember: "explicit is better than implicit" is part of the zen of python.

提交回复
热议问题