## Listing Python modules and getting help docs from Excel

Python functions include detailed help documentation but to access this you need the full path to the function, including the names of all code modules and submodules. This post looks at how this information can be found using Excel with pyxll, using the Scipy and Numpy libraries as examples.

As well as pyxll the code requires the inspect, importlib, pkgutil, and numpy libraries:

```from inspect import getmembers, isfunction, getdoc, ismodule, signature
import importlib as imp
import pkgutil

import numpy as np

from pyxll import xl_func, xl_arg, xl_return
```

Example code samples for listing functions from modules include the use of the functions inspect.getmembers and pkgutil.iter_modules. The code below allows either of these functions to be used, and returns either the full output, or just the names of listed functions:

```@xl_func
def get_modlist(modname, out=1, out2 = 1):
mod = imp.import_module(modname)
if out == 1:
memb = getmembers(mod, ismodule)
namelist =  [submod for submod in memb if ismodule(submod)]
else:
memb = list(pkgutil.iter_modules(mod.__path__))
namelist =  [submod for submod in memb if submod == True  ]
if out2 == 1:
return memb
else:
return namelist
```

This function was found to give different results with Numpy and Scipy. Using getmembers on the top level Scipy module returned no results, but Numpy returned all available module names:

Using pkgutil.iter_module returns the available modules in Scipy and are indicated with TRUE in the third column, but for Numpy all the modules are indicated as FALSE:

The code below uses pkgutil.iter for the top level Scipy module, and getmembers for all other cases:

```@xl_func
def get_modules(modname):
try:
mod = imp.import_module(modname)
except:
return []
if modname == 'scipy':
memb = list(pkgutil.iter_modules(mod.__path__))
namelist =  [submod for submod in memb if submod == True  ]
else:
memb = getmembers(mod, ismodule)
namelist =  [submod for submod in memb]
return namelist
```

The screenshot below shows this function displaying scipy modules and 5 levels of submodule:

For any selected submodule all the available functions can be listed with the get_funcs function:

```@xl_func
def get_funcs(modname,  searchstring =''):
lentxt = len(searchstring)
mod = imp.import_module(modname)
memb = getmembers(mod)
namelist =  [func for func in memb if ((isfunction(func) or type(func) == np.ufunc) and func[0:lentxt] == searchstring)]
return namelist
```

This function will display all functions included in the sub-module, or optionally a search string may be used:

From the list of functions, one can be chosen to display the built-in help with the Get_Docs function:

```@xl_func
@xl_arg('afunc', 'str')
@xl_arg('modname', 'str')
@xl_return('numpy_column<str>')
def get_docs(afunc, modname):
try:
mod = imp.import_module(modname)
afun = getattr(mod, afunc)
doc = getdoc(afun).split('\n')
except:
doc = ''
return np.array([doc])
```

With recent versions of Excel this function will return a dynamic array, automatically resizing to display the full extent of the text:
{"ft_lang_label":"__label__en","ft_lang_prob":0.57399875,"math_prob":0.7392406,"size":3097,"snap":"2022-40-2023-06","text_gpt3_token_len":770,"char_repetition_ratio":0.14645974,"word_repetition_ratio":0.078299776,"special_character_ratio":0.23700355,"punctuation_ratio":0.13153154,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9723937,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-29T08:58:31Z\",\"WARC-Record-ID\":\"<urn:uuid:fa062233-fdc9-4a51-8b4f-1ffd1c41b0a5>\",\"Content-Length\":\"145708\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:37317c45-7b32-48e3-85cb-dfd9390573ed>\",\"WARC-Concurrent-To\":\"<urn:uuid:23687d80-febd-466a-82fc-4684081ec464>\",\"WARC-IP-Address\":\"192.0.78.24\",\"WARC-Target-URI\":\"https://newtonexcelbach.com/2022/05/12/listing-python-modules-and-getting-help-docs-from-excel/\",\"WARC-Payload-Digest\":\"sha1:WWUXFZQ7EWMQRPGNXD3JNM4AFFYIDMMY\",\"WARC-Block-Digest\":\"sha1:S5QAUYV2I37KJ3J2MMQKAMKDLUO3KOCV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499710.49_warc_CC-MAIN-20230129080341-20230129110341-00825.warc.gz\"}"}