How to find a import module dependency for a given variable in a source code in python

Viewed 135

I am trying to identify the import module name which is dependent by a variable in the script.

ex:

import module1

import module2

x = module1.func()
    
y = x.func()

given y, my output should be y -> x -> module1 and the respective line num.

Can someone help me figure this out? Just now started exploring this with AST.

2 Answers

Just using ast is not going to be helpful in a lot of situations. You need to include some kind of (simulated) execution, akin of exec().

For example, what can the ast tell from this Python program, which outputs a string of digits? The identifier "digits" seems to come out of thin air in the syntax tree.

exec('from string import digits')
mystr = digits
print(mystr)

And, to support the argument of execution needed, the following program would have to result in output nums -> int, but just using a syntax tree, it would probably output nums -> string | int.

import string
nums = string.digits
if True:
    nums = 0
print(nums)

Now, what you are asking can still be done, but rather than solve it with abstract syntax trees, you would have more success using trace as a starting point, and enhance the data collected in the various tracing calls.

use __name__ attribute

>>> import numpy
>>> x = numpy
>>> y = x.__name__
>>> y
'numpy'

Edit:

import numpy

arr = []

def a(strofwanted, library, varname):
    global arr
    arr.append([varname, library])
    return exec(strofwanted)

x = a("numpy.array([1, 2])", "numpy", "x")
>>>x
[1,2]
>>>arr
[["x", "numpy"]]
Related