查找正在导入的 Python 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1997449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:31:08  来源:igfitidea点击:

Find which python modules are being imported

pythonpackage

提问by John Jiang

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

从应用程序中使用的特定包中查找所有 python 模块的简单方法是什么?

采纳答案by Ned Batchelder

sys.modulesis a dictionary mapping module names to modules. You can examine its keys to see imported modules.

sys.modules是一个将模块名称映射到模块的字典。您可以检查其键以查看导入的模块。

See: http://docs.python.org/library/sys.html#sys.modules

请参阅:http: //docs.python.org/library/sys.html#sys.modules

回答by Alex Martelli

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

您可以使用python -v,它将发出有关每个导入模块的消息:

$ 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 grepthe modules of interest from among this large list!-)

...等等等等。当然,您可以稍后grep从这个大列表中选择感兴趣的模块!-)

回答by Pablo Antonio

I think modulefinderis what you're looking for. You can use modulefinder.pydirectly, running it as a script as is described there, or you can import the module and then create a reportusing the modulefinder.ModuleFinderclass.

我认为modulefinder就是你要找的。您可以modulefinder.py直接使用,按照此处所述将其作为脚本运行,或者您可以导入模块,然后使用modulefinder.ModuleFinder该类创建报告

回答by Peter Hansen

A real simple method is to delete all .pyc files from the package or folder, and then run the application. Once you've played a bit, do a directory listing and see which files have .pyc files now. Those are modules which were imported by the application.

一个真正简单的方法是从包或文件夹中删除所有 .pyc 文件,然后运行应用程序。一旦你玩了一点,做一个目录列表,看看现在哪些文件有 .pyc 文件。这些是由应用程序导入的模块。

(Note: the __main__module, whichever one you invoke as the "main" script, never gets compiled, so you should not expect to see a .pyc file for it unless something imported it from within the application. This is often a sign of a problem if it does happen.)

(注意: __main__模块,无论你调用哪个作为“主”脚本,永远不会被编译,所以你不应该期望看到它的 .pyc 文件,除非从应用程序中导入它。这通常是一个标志如果确实发生了问题。)