Home:ALL Converter>How to find all child modules in Python?

How to find all child modules in Python?

Ask Time:2009-05-27T02:25:15         Author:Soviut

Json Formatter

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 child modules.

I'm building a library of tools for an existing 3D application. Each tool has its own menu item and sub menus. I'd like the tool to be responsible for creating its own menus as many of them change based on context and templates. I'd like my base module to be able to find all child modules and check for a create_menu() function and call it if it finds it.

What is the easiest way to discover all child modules?

Author:Soviut,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/912025/how-to-find-all-child-modules-in-python
deno :

When I was a kind and just beginning programming in Python I've written this for my modular IRC bot:\n\n\n # Load plugins\n\n _plugins = []\n\n def ifName(name):\n try:\n return re.match('([^_.].+)\\.[^.]+', a).group(1)\n except:\n return None\n\n def isValidPlugin(obj):\n from common.base import PluginBase\n try:\n if obj.__base__ == PluginBase:\n return True\n else:\n return False\n except:\n return False\n\n plugin_names = set(ifilter(lambda a: a!=None, [ifName(a) for a in os.listdir(os.path.join(os.getcwd(), 'plugins'))]))\n for plugin_name in plugin_names:\n try:\n plugin = __import__('plugins.'+plugin_name, fromlist=['plugins'])\n valid_plugins = filter(lambda a: isValidPlugin(a), [plugin.__getattribute__(a) for a in dir(plugin)])\n _plugins.extend(valid_plugins)\n except Exception, e:\n logger.exception('Error loading plugin %s', plugin_name)\n\n # Run plugins\n\n _plugins = [klass() for klass in _plugins]\n\n\nIt's not secure or \"right\" way, but maybe it we'll be useful nevertheless. It's very old code so please don't beat me.",
2009-05-26T18:37:09
Jürgen Strobel :

The solution above traversing the filesystem for finding submodules is ok as long as you implement every plugin as a filesystem based module.\n\nA 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.\n\nRemember: \"explicit is better than implicit\" is part of the zen of python.",
2011-09-07T17:48:02
Don Kirkby :

pkgutil.walk_packages() seems to be the right way to do this. Here's how I found a list of the available modules, and then imported one of them:\n$ mkdir foo\n$ touch foo/__init__.py\n$ touch foo/bar.py\n$ python\nPython 3.8.2 (default, Jul 16 2020, 14:00:26) \n[GCC 9.3.0] on linux\nType "help", "copyright", "credits" or "license" for more information.\n>>> import foo\n>>> from pkgutil import walk_packages\n>>> list(walk_packages(foo.__path__))\n[ModuleInfo(module_finder=FileFinder('/home/don/git/zero-play/zero_play/foo'), name='bar', ispkg=False)]\n>>> from importlib import import_module\n>>> bar = import_module('foo.bar')\n\n",
2020-09-04T17:30:33
muhuk :

You can try globbing the directory:\n\nimport os\nimport glob\n\nmodules = glob.glob(os.path.join('/some/path/to/modules', '*.py'))\n\n\nThen you can try importing them:\n\nchecked_modules\nfor module in modules:\n try:\n __import__(module, globals(), locals()) # returns module object\n except ImportError:\n pass\n else:\n checked_modules.append(module)\n",
2009-05-26T18:41:25
Ross Patterson :

I think the best way to do this sort of plugin thing is using entry_points and the API for querying them.",
2011-09-06T13:44:00
yy