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 eitheris
,in
, orstartswith
.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 wordedpkg
.in
: Returns modules that contains the stringpkg
.startswith
: Return modules that starts with the passedpkg
name.
When
imports
is set toFalse
this function returns aDict[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 toTrue
this function returns aDict[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