Return a list of imported Python modules used in a script?

后端 未结 13 2244
深忆病人
深忆病人 2020-11-30 00:23

I am writing a program that categorizes a list of Python files by which modules they import. As such I need to scan the collection of .py files ad return a list of which mod

13条回答
  •  野性不改
    2020-11-30 01:15

    You might want to try dis (pun intended):

    import dis
    from collections import defaultdict
    from pprint import pprint
    
    statements = """
    from __future__ import (absolute_import,
                            division)
    import os
    import collections, itertools
    from math import *
    from gzip import open as gzip_open
    from subprocess import check_output, Popen
    """
    
    instructions = dis.get_instructions(statements)
    imports = [__ for __ in instructions if 'IMPORT' in __.opname]
    
    grouped = defaultdict(list)
    for instr in imports:
        grouped[instr.opname].append(instr.argval)
    
    pprint(grouped)
    

    outputs

    defaultdict(,
                {'IMPORT_FROM': ['absolute_import',
                                 'division',
                                 'open',
                                 'check_output',
                                 'Popen'],
                 'IMPORT_NAME': ['__future__',
                                 'os',
                                 'collections',
                                 'itertools',
                                 'math',
                                 'gzip',
                                 'subprocess'],
                 'IMPORT_STAR': [None]})
    

    Your imported modules are grouped['IMPORT_NAME'].

提交回复
热议问题