Home:ALL Converter>List all the modules that are part of a python package?

List all the modules that are part of a python package?

Ask Time:2009-11-10T20:47:30         Author:static_rtti

Json Formatter

Is there a straightforward way to find all the modules that are part of a python package? I've found this old discussion, which is not really conclusive, but I'd love to have a definite answer before I roll out my own solution based on os.listdir().

Author:static_rtti,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/1707709/list-all-the-modules-that-are-part-of-a-python-package
user1767754 :

I was looking for a way to reload all submodules that I'm editing live in my package. It is a combination of the answers/comments above, so I've decided to post it here as an answer rather than a comment.\n\npackage=yourPackageName\nimport importlib\nimport pkgutil\nfor importer, modname, ispkg in pkgutil.walk_packages(path=package.__path__, prefix=package.__name__+'.', onerror=lambda x: None):\n try:\n modulesource = importlib.import_module(modname)\n reload(modulesource)\n print(\"reloaded: {}\".format(modname))\n except Exception as e:\n print('Could not load {} {}'.format(modname, e))\n",
2019-11-03T03:46:05
u0b34a0f6ae :

Yes, you want something based on pkgutil or similar -- this way you can treat all packages alike regardless if they are in eggs or zips or so (where os.listdir won't help).\n\nimport pkgutil\n\n# this is the package we are inspecting -- for example 'email' from stdlib\nimport email\n\npackage = email\nfor importer, modname, ispkg in pkgutil.iter_modules(package.__path__):\n print \"Found submodule %s (is a package: %s)\" % (modname, ispkg)\n\n\nHow to import them too? You can just use __import__ as normal:\n\nimport pkgutil\n\n# this is the package we are inspecting -- for example 'email' from stdlib\nimport email\n\npackage = email\nprefix = package.__name__ + \".\"\nfor importer, modname, ispkg in pkgutil.iter_modules(package.__path__, prefix):\n print \"Found submodule %s (is a package: %s)\" % (modname, ispkg)\n module = __import__(modname, fromlist=\"dummy\")\n print \"Imported\", module\n",
2009-11-10T12:58:55
unutbu :

The right tool for this job is pkgutil.walk_packages.\n\nTo list all the modules on your system:\n\nimport pkgutil\nfor importer, modname, ispkg in pkgutil.walk_packages(path=None, onerror=lambda x: None):\n print(modname)\n\n\nBe aware that walk_packages imports all subpackages, but not submodules.\n\nIf you wish to list all submodules of a certain package then you can use something like this:\n\nimport pkgutil\nimport scipy\npackage=scipy\nfor importer, modname, ispkg in pkgutil.walk_packages(path=package.__path__,\n prefix=package.__name__+'.',\n onerror=lambda x: None):\n print(modname)\n\n\niter_modules only lists the modules which are one-level deep. \nwalk_packages gets all the submodules.\nIn the case of scipy, for example, walk_packages returns \n\nscipy.stats.stats\n\n\nwhile iter_modules only returns\n\nscipy.stats\n\n\nThe documentation on pkgutil (http://docs.python.org/library/pkgutil.html)\ndoes not list all the interesting functions defined in \n/usr/lib/python2.6/pkgutil.py.\n\nPerhaps this means the functions are not part of the \"public\" interface and are subject to change.\n\nHowever, at least as of Python 2.6 (and perhaps earlier versions?)\npkgutil comes with a walk_packages method which recursively walks through all the\nmodules available.",
2009-11-10T15:15:48
DarinP :

This works for me:\n\nimport types\n\nfor key, obj in nltk.__dict__.iteritems():\n if type(obj) is types.ModuleType: \n print key\n",
2013-03-30T20:24:30
yy