How to find all uses of a python function or variable in a python package
<p>I'm trying to map out the uses/causes of functions and variables in a python package at the function level. There are several modules where functions/variables are used in other functions, and I'd like to create a dictionary that looks something like:</p>

<pre><code>{'function_name':{'uses': [...functions used in this function...],
                  'causes': [...functions that use this function...]},
 ...
}
</code></pre>

<p>The functions that I am referring to need to be defined in modules of the package.</p>

<p>How would I start on this? I know that I can iterate through the package <code>__dict__</code> and test for functions defined in the package by doing:</p>

<pre><code>import package

import inspect
import types

for name, obj in vars(package).items():
    if isinstance(obj, types.FunctionType):
        module, *_ = inspect.getmodule(obj).__name__.split('.')
        if module == package.__name__:
            # Now that function is obtained need to find usages or functions used within it
</code></pre>

<p>But after that I need to find the functions used within the current function. How can this be done? Is there something already developed for this type of work? I think that profiling libraries might have to do something similar to this.</p>