如何获取所有 Python 标准库模块的列表 [英] How can I get a list of all the Python standard library modules
问题描述
除了标准库之外,我想要类似 sys.builtin_module_names
的东西.其他不起作用的事情:
I want something like sys.builtin_module_names
except for the standard library. Other things that didn't work:
sys.modules
- 只显示已经加载的模块sys.prefix
- 包含非标准库模块的路径并且似乎在 virtualenv 中不起作用.
sys.modules
- only shows modules that have already been loadedsys.prefix
- a path that would include non-standard library modules and doesn't seem to work inside a virtualenv.
我想要这个列表的原因是我可以将它传递给 --ignore-module
或 --ignore-dir
命令行选项 跟踪
http://docs.python.org/library/trace.html
The reason I want this list is so that I can pass it to the --ignore-module
or --ignore-dir
command line options of trace
http://docs.python.org/library/trace.html
所以最终,我想知道如何在使用 trace
或 sys.settrace
时忽略所有标准库模块.
So ultimately, I want to know how to ignore all the standard library modules when using trace
or sys.settrace
.
我希望它在 virtualenv 中工作.http://pypi.python.org/pypi/virtualenv
I want it to work inside a virtualenv. http://pypi.python.org/pypi/virtualenv
我希望它适用于所有环境(即跨操作系统、virtualenv 内部和外部.)
I want it to work for all environments (i.e. across operating systems, inside and outside of a virtualenv.)
推荐答案
为什么不自己找出标准库的一部分?
Why not work out what's part of the standard library yourself?
import distutils.sysconfig as sysconfig
import os
std_lib = sysconfig.get_python_lib(standard_lib=True)
for top, dirs, files in os.walk(std_lib):
for nm in files:
if nm != '__init__.py' and nm[-3:] == '.py':
print os.path.join(top, nm)[len(std_lib)+1:-3].replace(os.sep, '.')
给予
abc
aifc
antigravity
--- a bunch of other files ----
xml.parsers.expat
xml.sax.expatreader
xml.sax.handler
xml.sax.saxutils
xml.sax.xmlreader
xml.sax._exceptions
如果您需要避免使用非标准库模块,您可能需要添加一个检查来避免 site-packages
.
You'll probably want to add a check to avoid site-packages
if you need to avoid non-standard library modules.
这篇关于如何获取所有 Python 标准库模块的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!