Package: unbox
unboxFinding imports in codeTo install:pip install unboxWhat's hereLots of little goodies to help you analyze the imports of your, or others' code.Getting a list of missing dependencies>>>fromunboximportprint_missing_names>>>importsome_module# doctest: +SKIP>>>print_missing_names(some_module)# doctest: +SKIPSoundFilei2librosapyttsx3slinkSeeing what the dependencies of a package are, before installing itSimply get a list of dependencies for a package from PyPI.>>>importunbox>>>unbox.dependencies_from_pypi('pandas')['numpy','numpy','python-dateutil','pytz','tzdata']But you have control over the requirements that are returned,
and how they are returned:>>>it=unbox.dependencies_from_pypi(...'pandas',...requirement_filter=lambdax:True,# don't filter any requirements...requirement_trans=lambdax:x,# as is...egress=lambdax:x# just get the iterator as is...)>>>next(it)'numpy>=1.22.4; python_version < "3.11"'>>>list(it)[-1]'zstandard>=0.17.0; extra == "all"'A dict-like interfaceThe base ofunboxis just thedolinterface tofindimports, which then allows us to offer a bunch of functionalities easily.Say you wanted to know what dol was made of.
The dol way of doing this is to make aMapping(i.e. a key-value dict-like interface),
and then do what you do with dicts...>>>importdol>>>importunbox>>>s=unbox.ModuleNamesImportedByModule(dol)# make a store containing the modules of the `dol` package>>># Now wee how you can do things you do with dicts>>>len(s)15>>>list(s)['dol.__init__','dol.appendable','dol.base','dol.caching','dol.core','dol.dig','dol.errors','dol.filesys','dol.mixins','dol.naming','dol.paths','dol.signatures','dol.sources','dol.trans','dol.util']>>>'dol.appendable'ins>>># The values of `s` are sets of modules imported by a module.>>>s['dol.appendable']# what does dol.appendable import?{'collections.abc','dol.trans','time','types','typing'}Check outModulesImportedByModulealso, which gives you aMappingwith module objects
as keys, andfindimports.ImportInfoinstances as values.imports_forAs an example of what you can do with this set up, have a look atimports_for.
Or don't have a look; just use it, since it's quite useful.fromunboximportimports_forimportwaveassertimports_for(wave)=={'warnings','builtins','sys','audioop','chunk','struct','collections'}At it's base, imports_for gives you a generator of import names.
With thepostargument (defaulted toset) you can specify a callable that can produce the output
you want; whether you want to filter the items, count them, order them, etc.We curried a few common ones for you, for your convenience:fromunboximportimports_forimports_for.counter# imported names and their countsimports_for.most_common# imported names and their counts, ordered by most commonimports_for.first_level# set for imported first level names (e.g. 'os' instead of 'os.path.etc.)imports_for.first_level_count# count of imported first level names (e.g. 'os' instead of 'os.path.etc.)imports_for.third_party# imported (first level) names that are not builtin names (most probably third party packages)"Collections of python namesCheck out the contents of these collections:fromunboximport(builtin_module_names,scanned_standard_lib_names,all_accessible_modules,all_accessible_pkg_names,all_accessible_non_pkg_module_names,builtin_obj_names,python_names)For example,builtin_module_nameswill be a set of names that aredocumentedandimportable on your system.Thescanned_standard_lib_namesset is similar, but the names are obtained by scanning
the local standard library file names -- so include things like easter eggs (this,antigravity).all_accessible_moduleswill be the list of all modules accessible in your python path.And so on...