Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List submodules of a python module

Tags:

python

module

How to get the name/list of all submodules of an already imported Python module? (Not the external imported module/packages, just that one in the same folder as module sub-folder).

I am using

import inspect
print([o[0] for o in inspect.getmembers(module_imported) if inspect.ismodule(o[1])])

but this print also the system modules imported and files in the main module folder.

like image 907
hildogjr Avatar asked Dec 28 '17 02:12

hildogjr


People also ask

How do I list all functions in a Python module?

We can list down all the functions present in a Python module by simply using the dir() method in the Python shell or in the command prompt shell.

What are submodules in Python?

In my limited experience, modules with submodules are simply folders with a __init__.py file, while modules with functions/classes are actual python files.

How do I display the contents of a Python module?

We've then used the dir () function to display the attributes provided by the module. Here is a brief description of the attributes: Bye, Welcome – the functions provided by the module. __builtins__ – a listing of all the built-in attributes that are accessible from the module.

How do I get a list of Python libraries?

The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages. You can also use the ActiveState Platform's command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command.


1 Answers

Use pkgutil, though it doesn't always work with pyinstaller.

pkgutil.iter_modules(imported_module.__path__))
like image 77
GraphicalDot Avatar answered Oct 29 '22 17:10

GraphicalDot