查找正在导入的 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
Find which python modules are being imported
提问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.modules
is a dictionary mapping module names to modules. You can examine its keys to see imported modules.
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 grep
the modules of interest from among this large list!-)
...等等等等。当然,您可以稍后grep
从这个大列表中选择感兴趣的模块!-)
回答by Pablo Antonio
I think modulefinderis what you're looking for. You can use modulefinder.py
directly, running it as a script as is described there, or you can import the module and then create a reportusing the modulefinder.ModuleFinder
class.
我认为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 文件,除非从应用程序中导入它。这通常是一个标志如果确实发生了问题。)