Python 2's `exceptions` module is missing in Python3, where did its contents go?

13.8k views Asked by At

A friend mentioned that with Python 2, (assuming you have it on your path environment variable, on the commandline)

$ pydoc exceptions 

is very useful and knowing it should save him a few minutes of web lookup time a week. I Google the exceptions hierarchy about once a week myself, so this was a helpful reminder for me as well. It is the same documentation that you get with

>>> import exceptions
>>> help(exceptions) 

in Python 2, because pydoc uses the exceptions module to provide the online documentation.

However, he noted this doesn't work with Python 3. This is because the exceptions module doesn't exist in Python 3.

I can see why he likes it - it shows the very useful exceptions hierarchy for quick perusal, and I reference it myself frequently. But the exceptions module with the resulting builtin documentation is missing from Python 3! How can he replace it?

To ensure that Stackoverflow has the answer to this question, in general:

How does one replace the contents of the exceptions module in Python 2 when moving to Python 3?

1

There are 1 answers

0
Russia Must Remove Putin On BEST ANSWER

As a prefatory remark, let me say that in most cases, you don't need the contents of Python 2's exceptions module, as they are found in the __builtin__ global namespace in all modules. However, we want it for the online documentation.

In this case, the simple answer is that the contents of Python 2's exceptions module has been moved, for consistency, to the builtins module.

In a Python 3 shell:

>>> import builtins
>>> help(builtins)

will provide the same documentation.

And if you have Python 3's directory on your path (that is, you can type python on your command line and it brings up the Python 3 shell) then with

$ pydoc builtins

We'll get the same.

If you want to test this, but don't have Python 3's pydoc on your path, you can test it in your Python3.x directory with both of the following, I got the same output:

$ python3 pydoc.py builtins
$ ./pydoc.py builtins

And you'll see Python 3's exception hierarchy (shown below), along with the rest of the documentation:

    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

A commenter says:

Would be nice to include a python 2/3 compatibility solution. My use case was a list of all exception names for a syntax highlighter.

I would do something like this for compatibility:

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))

You could expect builtins to have every built-in exception in Python 3, just as exceptions did in Python 2 - it would just also have the rest of the builtins as well.

The exceptions_list could be your canonical list of all builtin exceptions.