import types


def list_module_versions(glob, return_dict = False):
    """
    Prints the versions of all loaded modules/packages in a script or notebook.

    Parameters
    ----------
    glob : dict 
        output of the globals() function call.
    return_dict : bool, optional
        Parameter to decide if function should return versions dict. The default is False.    
    
    Returns
    -------
    versions_dict : dict, optional
        Dict with module names as keys and version numbers as values.

    """
    versions_dict = {}
    
    for name, val in glob.items():
        if isinstance(val, types.ModuleType) and "__version__" in val.__dict__:
            print(val.__name__, val.__version__)
            versions_dict[val.__name__] = val.__version__
            
    if return_dict is True:
        return versions_dict