Find which python modules are being imported

后端 未结 4 802
有刺的猬
有刺的猬 2021-01-31 08:16

What\'s an easy way of finding all the python modules from a particular package that are being used in an application?

4条回答
  •  佛祖请我去吃肉
    2021-01-31 08:44

    You could use python -v, which will emit messages about every imported module:

    $ echo 'print "hello world"' > helo.py
    $ python -v helo.py
    # installing zipimport hook
    import zipimport # builtin
    # installed zipimport hook
    # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site.py
    import site # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site.pyc
    # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.py
    import os # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.pyc
    import posix # builtin
    # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py
    import posixpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.pyc
    

    ...and so on, and so forth. Of course you can later grep the modules of interest from among this large list!-)

提交回复
热议问题