Вы можете получить байт-код функции и затем проанализировать имя модуля, где код операции "IMPORT_NAME"
import dis
from subprocess import PIPE, run
def a():
import pandas
bytecode = dis.Bytecode(a)
modules = [instr.argval for instr in bytecode if instr.opname == "IMPORT_NAME"]
for module in modules:
command = f"pip show {module} | grep Version"
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
print(module, result.stdout)