如何获取所有Python标准库模块的列表

sal*_*ane 37 python std virtualenv

我想要sys.builtin_module_names除了标准库以外的东西.其他不起作用的东西:

  • sys.modules - 仅显示已加载的模块
  • sys.prefix - 一个包含非标准库模块EDIT的路径:并且似乎不能在virtualenv中工作.

我想要这个列表的原因是我可以将它传递给http://docs.python.org/library/trace.html--ignore-module--ignore-dir命令行选项trace

所以最终,我想知道如何在使用trace或时忽略所有标准库模块sys.settrace.

编辑:我希望它在virtualenv中工作.http://pypi.python.org/pypi/virtualenv

编辑2:我希望它适用于所有环境(即跨越操作系统,在virtualenv的内部和外部.)

小智 28

如果有人在2015年仍在阅读这篇文章,我遇到了同样的问题,并且不喜欢任何现有的解决方案.所以,我通过编写一些代码来强制它,以便在官方Python文档中删除标准库页面的TOC.我还构建了一个简单的API来获取标准库列表(适用于Python版本2.6,2.7,3.2,3.3和3.4).

包在这里,它的用法很简单:

>>> from stdlib_list import stdlib_list
>>> libraries = stdlib_list("2.7")
>>> libraries[:10]
['AL', 'BaseHTTPServer', 'Bastion', 'CGIHTTPServer', 'ColorPicker', 'ConfigParser', 'Cookie', 'DEVICE', 'DocXMLRPCServer', 'EasyDialogs']
Run Code Online (Sandbox Code Playgroud)


Cas*_*par 14

为什么不自己弄清楚标准库的哪些部分呢?

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, '.')
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

编辑:您可能希望添加一个检查,以避免site-packages您需要避免非标准库模块.

  • 事实证明,如果我的virtualenv被激活,当我调用Python解释器时,我的当前工作目录是`/ usr/lib/python2.6`,`sysconfig.get_python_lib(standard_lib = True)`返回我的virtualenv的路径(例如`〜/ .virtualenvs/myenv/LIB/python2.6`).但是,如果我当前的工作目录不是`/ usr/lib/python2.6`,它将返回正确的路径`/ usr/lib/python2.6`.所以我昨晚没有吸毒. (2认同)
  • 这是一个很好的解决方案,但是它不包含诸如sys之类的核心库。 (2认同)
  • 这不会打印诸如“datetime”、“itertools”或“math”之类的模块,这些模块位于“lib-dynload/”目录中并且具有“.so”扩展名(无论如何在我的机器上)。也没有打印“importlib”,它位于“importlib/__init__.py”中 (2认同)

小智 10

看看这个, https://docs.python.org/3/py-modindex.html 他们为标准模块制作了一个索引页面.


小智 10

在 Python 3.10 上,现在有sys.stdlib_module_names


Ada*_*ers 6

这是Caspar答案的改进,它不是跨平台的,并且错过了顶级模块(例如email),动态加载的模块(例如array)和核心内置模块(例如sys):

import distutils.sysconfig as sysconfig
import os
import sys

std_lib = sysconfig.get_python_lib(standard_lib=True)

for top, dirs, files in os.walk(std_lib):
    for nm in files:
        prefix = top[len(std_lib)+1:]
        if prefix[:13] == 'site-packages':
            continue
        if nm == '__init__.py':
            print top[len(std_lib)+1:].replace(os.path.sep,'.')
        elif nm[-3:] == '.py':
            print os.path.join(prefix, nm)[:-3].replace(os.path.sep,'.')
        elif nm[-3:] == '.so' and top[-11:] == 'lib-dynload':
            print nm[0:-3]

for builtin in sys.builtin_module_names:
    print builtin
Run Code Online (Sandbox Code Playgroud)

这仍然不完美,因为它会错过像os.path内部os.py以平台相关的方式通过代码定义的内容import posixpath as path,但它可能会达到你想要的那样好,记住Python是一种动态语言,你可以在实际上在运行时定义之前,我们真的知道定义了哪些模块.


wim*_*wim 5

这是2011年问题的2014年答案-

isort的作者(清理进口的工具)必须解决同样的问题,才能满足pep8要求,即在第三方进口之前必须订购核心库进口。

我一直在使用此工具,它似乎运行良好。您可以place_module在file中使用该方法isort.py,因为它是开源的,我希望作者不要介意我在此处复制逻辑:

def place_module(self, moduleName):
    """Tries to determine if a module is a python std import, third party import, or project code:

    if it can't determine - it assumes it is project code

    """
    if moduleName.startswith("."):
        return SECTIONS.LOCALFOLDER

    index = moduleName.find('.')
    if index:
        firstPart = moduleName[:index]
    else:
        firstPart = None

    for forced_separate in self.config['forced_separate']:
        if moduleName.startswith(forced_separate):
            return forced_separate

    if moduleName == "__future__" or (firstPart == "__future__"):
        return SECTIONS.FUTURE
    elif moduleName in self.config['known_standard_library'] or \
            (firstPart in self.config['known_standard_library']):
        return SECTIONS.STDLIB
    elif moduleName in self.config['known_third_party'] or (firstPart in self.config['known_third_party']):
        return SECTIONS.THIRDPARTY
    elif moduleName in self.config['known_first_party'] or (firstPart in self.config['known_first_party']):
        return SECTIONS.FIRSTPARTY

    for prefix in PYTHONPATH:
        module_path = "/".join((prefix, moduleName.replace(".", "/")))
        package_path = "/".join((prefix, moduleName.split(".")[0]))
        if (os.path.exists(module_path + ".py") or os.path.exists(module_path + ".so") or
           (os.path.exists(package_path) and os.path.isdir(package_path))):
            if "site-packages" in prefix or "dist-packages" in prefix:
                return SECTIONS.THIRDPARTY
            elif "python2" in prefix.lower() or "python3" in prefix.lower():
                return SECTIONS.STDLIB
            else:
                return SECTIONS.FIRSTPARTY

    return SECTION_NAMES.index(self.config['default_section'])
Run Code Online (Sandbox Code Playgroud)

显然,您需要在类和设置文件的上下文中使用此方法。从本质上讲,这只是已知核心lib导入的静态列表的后备。

# Note that none of these lists must be complete as they are simply fallbacks for when included auto-detection fails.
default = {'force_to_top': [],
           'skip': ['__init__.py', ],
           'line_length': 80,
           'known_standard_library': ["abc", "anydbm", "argparse", "array", "asynchat", "asyncore", "atexit", "base64",
                                      "BaseHTTPServer", "bisect", "bz2", "calendar", "cgitb", "cmd", "codecs",
                                      "collections", "commands", "compileall", "ConfigParser", "contextlib", "Cookie",
                                      "copy", "cPickle", "cProfile", "cStringIO", "csv", "datetime", "dbhash", "dbm",
                                      "decimal", "difflib", "dircache", "dis", "doctest", "dumbdbm", "EasyDialogs",
                                      "errno", "exceptions", "filecmp", "fileinput", "fnmatch", "fractions",
                                      "functools", "gc", "gdbm", "getopt", "getpass", "gettext", "glob", "grp", "gzip",
                                      "hashlib", "heapq", "hmac", "imaplib", "imp", "inspect", "itertools", "json",
                                      "linecache", "locale", "logging", "mailbox", "math", "mhlib", "mmap",
                                      "multiprocessing", "operator", "optparse", "os", "pdb", "pickle", "pipes",
                                      "pkgutil", "platform", "plistlib", "pprint", "profile", "pstats", "pwd", "pyclbr",
                                      "pydoc", "Queue", "random", "re", "readline", "resource", "rlcompleter",
                                      "robotparser", "sched", "select", "shelve", "shlex", "shutil", "signal",
                                      "SimpleXMLRPCServer", "site", "sitecustomize", "smtpd", "smtplib", "socket",
                                      "SocketServer", "sqlite3", "string", "StringIO", "struct", "subprocess", "sys",
                                      "sysconfig", "tabnanny", "tarfile", "tempfile", "textwrap", "threading", "time",
                                      "timeit", "trace", "traceback", "unittest", "urllib", "urllib2", "urlparse",
                                      "usercustomize", "uuid", "warnings", "weakref", "webbrowser", "whichdb", "xml",
                                      "xmlrpclib", "zipfile", "zipimport", "zlib", 'builtins', '__builtin__'],
           'known_third_party': ['google.appengine.api'],
           'known_first_party': [],
Run Code Online (Sandbox Code Playgroud)

---剪-

在迷失了isort模块之前,我已经为自己编写了这个工具一个小时,所以我希望这也可以帮助其他人避免重新发明轮子!