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?
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 thebuiltins
module.In a Python 3 shell:
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
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:
And you'll see Python 3's exception hierarchy (shown below), along with the rest of the documentation:
A commenter says:
I would do something like this for compatibility:
You could expect
builtins
to have every built-in exception in Python 3, just asexceptions
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.