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?
This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.
It's automatically imported when Python starts, and the exceptions are added to the _ _builtin_ _ module. In other words, you usually don't need to import this module. This is a Python module in 1.5. 2, and a built-in module in 2.0 and later.
Python looks for modules in “sys. It looks for a file called a_module.py in the directories listed in the variable sys.
As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With