pkgutil#

package#

from toolbox.pkgutil import package
search_package(pkg, method, imports)[source]#

Discover packages installed in the system.

Function iterates through all of the installed packages in the system and returns a dictionary with all modules that passes the search criteria passed by method.

Parameters
  • pkg (str) – Package name to search for.

  • method (str) – String that is either is, in, or startswith.

  • imports (bool) – Boolean that indicates whether or not to import the found package(s).

Raises

TypeError – The search method passed is invalid.

Note

The search method must be one of the following:

  • is: Returns modules that are exactly worded pkg.

  • in: Returns modules that contains the string pkg.

  • startswith: Return modules that starts with the passed pkg name.

When imports is set to False this function returns a Dict[str, str] where the key is the name of the module, and the value is the version installed on the system.

If imports is set to True this function returns a Dict[str, Module] where the key is the name of the module, and the value is the _imported_ module.

Example

from toolbox.pkgutil.package import search_package

print(search_package("toolbox", method="is"))
# >>> {'toolbox': '1.4.0'}

print(search_package("toolbox", method="is", imports=True))
# >>> {'toolbox': <module 'toolbox' from '.../toolbox/toolbox/__init__.py'>}
Return type

dict