I want something like sys.builtin_module_names
except for the standard library. Other things that didn’t work:
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.
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
.
So ultimately, I want to know how to ignore all the standard library modules when using trace
or sys.settrace
.
Advertisement
Answer
Why not work out what’s part of the standard library yourself?
JavaScript
x
8
1
import distutils.sysconfig as sysconfig
2
import os
3
std_lib = sysconfig.get_python_lib(standard_lib=True)
4
for top, dirs, files in os.walk(std_lib):
5
for nm in files:
6
if nm != '__init__.py' and nm[-3:] == '.py':
7
print os.path.join(top, nm)[len(std_lib)+1:-3].replace(os.sep, '.')
8
gives
JavaScript
1
11
11
1
abc
2
aifc
3
antigravity
4
--- a bunch of other files ----
5
xml.parsers.expat
6
xml.sax.expatreader
7
xml.sax.handler
8
xml.sax.saxutils
9
xml.sax.xmlreader
10
xml.sax._exceptions
11
Edit: You’ll probably want to add a check to avoid site-packages
if you need to avoid non-standard library modules.