I'm using inspect.getsource
to retrieve the source code of functions. However, since some variables are imported at top level of the module containing the functions, it is not returned by inspect.getsource
. For example, if we have
import numpy as np
def sqrt(x):
return np.sqrt(x)
then inspect.getsource(sqrt)
will not return the line import numpy as np
. Is it possible to automatically retrieve what np
here refers to? The import can also be other forms, such as from some_module import some_func
, from some_module import *
, import some_module as some_name
.
Right now I'm using this function in trace function called by sys.settrace
, so the solution can be both static (without running the code) or dynamic (when running the code, such as examining the frames).
Aucun commentaire:
Enregistrer un commentaire