Python3中缺少Python 2的"例外"模块,其内容是什么?

Aar*_*all 7 python exception built-in python-2.7 python-3.x

一位朋友提到使用Python 2,(假设你的路径环境变量,在命令行上)

$ pydoc exceptions 
Run Code Online (Sandbox Code Playgroud)

是非常有用的,知道它应该每周为他节省几分钟的网页查询时间.我自己每周一次谷歌例外层次结构,所以这对我来说也是一个有用的提醒.它与您相同的文档

>>> import exceptions
>>> help(exceptions) 
Run Code Online (Sandbox Code Playgroud)

在Python 2中,因为pydoc使用例外模块来提供在线文档.

但是,他指出这不适用于Python 3.这是因为exceptionsPython 3中不存在该模块.

我可以看出为什么他喜欢它 - 它显示了非常有用的异常层次结构,可以快速阅读,我经常自己引用它.但是exceptionsPython 3中缺少带有生成的内置文档的模块!他怎么能取而代之呢?

为了确保Stackoverflow能够回答这个问题,一般来说:

在迁移到Python 3时,如何替换Python 2中的异常模块的内容?

Aar*_*all 10

作为一个前言,我要说在大多数情况下,您不需要Python 2 exceptions模块的内容,因为它们可以__builtin__在所有模块的全局命名空间中找到.但是,我们希望它用于在线文档.

在这种情况下,简单的答案是exceptions,为了保持一致性,Python 2 模块的内容已经移动到builtins模块.

在Python 3 shell中:

>>> import builtins
>>> help(builtins)
Run Code Online (Sandbox Code Playgroud)

将提供相同的文件.

如果你的路径上有Python 3的目录(也就是说,你可以在你的命令行上键入python,它会调出Python 3 shell)然后使用

$ pydoc builtins
Run Code Online (Sandbox Code Playgroud)

我们会得到相同的.

如果你想测试这个,但是路径上没有Python 3的pydoc,你可以在Python3.x目录中使用以下两种方法测试它,我得到了相同的输出:

$ python3 pydoc.py builtins
$ ./pydoc.py builtins
Run Code Online (Sandbox Code Playgroud)

您将看到Python 3的异常层次结构(如下所示)以及其他文档:

    BaseException
        Exception
            ArithmeticError
                FloatingPointError
                OverflowError
                ZeroDivisionError
            AssertionError
            AttributeError
            BufferError
            EOFError
            ImportError
            LookupError
                IndexError
                KeyError
            MemoryError
            NameError
                UnboundLocalError
            OSError
                BlockingIOError
                ChildProcessError
                ConnectionError
                    BrokenPipeError
                    ConnectionAbortedError
                    ConnectionRefusedError
                    ConnectionResetError
                FileExistsError
                FileNotFoundError
                InterruptedError
                IsADirectoryError
                NotADirectoryError
                PermissionError
                ProcessLookupError
                TimeoutError
            ReferenceError
            RuntimeError
                NotImplementedError
            StopIteration
            SyntaxError
                IndentationError
                    TabError
            SystemError
            TypeError
            ValueError
                UnicodeError
                    UnicodeDecodeError
                    UnicodeEncodeError
                    UnicodeTranslateError
            Warning
                BytesWarning
                DeprecationWarning
                FutureWarning
                ImportWarning
                PendingDeprecationWarning
                ResourceWarning
                RuntimeWarning
                SyntaxWarning
                UnicodeWarning
                UserWarning
        GeneratorExit
        KeyboardInterrupt
        SystemExit
Run Code Online (Sandbox Code Playgroud)

评论者说:

很高兴包含一个python 2/3兼容性解决方案.我的用例是语法高亮显示的所有异常名称的列表.

为了兼容性,我会做这样的事情:

try:
    import exceptions
except ImportError:
    import builtins as exceptions

exceptions_list = sorted(n for n, e in vars(exceptions).items() 
                         if isinstance(e, type) and 
                            issubclass(e, BaseException))
Run Code Online (Sandbox Code Playgroud)

您可以期望builtins在Python 3中拥有每个内置异常,就像exceptions在Python 2中一样 - 它也将拥有其余的内置函数.

exceptions_list可以是所有内置异常的规范列表.