Python 2 module `exceptions` is missing in Python3, where is its content left?
A friend mentioned that as of Python 2 (assuming you have a path environment variable, on the command line)
$ pydoc exceptions
very helpful and knows that he should save him a few minutes of web searches a week. I google the exception hierarchy about once a week myself, so this was a helpful reminder for me. This is the same documentation you get with
>>> import exceptions
>>> help(exceptions)
in Python 2 because it pydoc
uses the exception module to provide online documentation.
However, he noted that this does not work with Python 3. This is because the module exceptions
does not exist in Python 3.
I can see why he likes it - it shows a very useful exception hierarchy for quick reading, and I refer to it often. But the module exceptions
with the resulting inline documentation is missing in Python 3! How can he replace him?
To ensure that Stackoverflow has an answer to this question, in general:
How do I replace the contents of an exception module in Python 2 when migrating to Python 3?
source to share
As a preliminary note, let me say that in most cases you don't need the contents of a Python 2 module exceptions
as they are in the global namespace __builtin__
across all modules. However, we want this for online documentation.
In this case, the simple answer is that the contents of the Python 2 module exceptions
have been moved for consistency into the module builtins
.
In Python 3 shell:
>>> import builtins
>>> help(builtins)
will provide the same documentation.
And if you have a Python 3 directory in your path (i.e. you can type python on the command line and display a Python 3 shell) then
$ pydoc builtins
We'll get the same thing.
If you want to test this, but you don't have Python 3 pydoc in your path, you can test it in your Python3.x directory with both of the following, I got the same result:
$ python3 pydoc.py builtins $ ./pydoc.py builtins
And you will see the Python 3 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
source to share