Pretty Error Output In Python
Tags: beginners, python, 100daysofpython
Published: 2021-09-01 10:15:10

_**I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to [visit my blog](https://blog.dennisokeeffe.com) and subscribe to my newsletter. [Follow me on Twitter](https://twitter.com/dennisokeeffe92).**_

> This is Day 11 of the #100DaysOfPython challenge.

This post will use the [`PrettyErrors`](https://github.com/onelivesleft/PrettyErrors) library to output more readable errors in Python.

It will build off the work done on learning [Python Fire](https://github.com/google/python-fire) and [`PyInquirer`](https://github.com/CITGuru/PyInquirer/) basics in a [previous blog post](https://blog.dennisokeeffe.com/blog/2021-07-29-cli-prompts-in-python).

## Prerequisites

1. Familiarity with [Pipenv](https://pipenv.pypa.io/en/latest/). See [here](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv) for my post on Pipenv.
2. Familiarity with [Python Fire](https://github.com/google/python-fire)
3. (optional) Familiarity with [`PyInquirer`](https://github.com/CITGuru/PyInquirer/)

## Getting started

While I will be using my [`hello-python-fire`](https://github.com/okeeffed/hello-python-fire) repo, the follow will work in any repo initialized with `pipenv`.

```s
# Add PrettyErrors
$ pipenv install pretty_errors
```

We are now able to begin throwing some example errors.

## Demonstrating the error

Python Fire requires valid input for method arguments for how it operates.

We will simply update the top of the code for `cli.py` to raise a very contrived exception:

```python
#!/usr/bin/env python
import fire
from PyInquirer import prompt


class IngestionStage(object):
    def run(self):
        raise Exception("Contrived example")
        return 'Ingesting! Nom nom nom...'
```

Now if we run `python cli.py ingestion run` we will see the following error:

```s
$ python cli.py ingestion run
Traceback (most recent call last):
  File "/Users/dennisokeeffe/code/blog-projects/hello-fire/cli.py", line 66, in <module>
    fire.Fire(Pipeline)
  File "/Users/dennisokeeffe/code/blog-projects/hello-fire/.venv/lib/python3.9/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/Users/dennisokeeffe/code/blog-projects/hello-fire/.venv/lib/python3.9/site-packages/fire/core.py", line 466, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/Users/dennisokeeffe/code/blog-projects/hello-fire/.venv/lib/python3.9/site-packages/fire/core.py", line 681, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/Users/dennisokeeffe/code/blog-projects/hello-fire/cli.py", line 8, in run
    raise Exception("Contrived example")
Exception: Contrived example
```

## Prettifying the error

To see Pretty Errors in action, all we need to do is import the package:

```py
#!/usr/bin/env python
import fire
from PyInquirer import prompt
import pretty_errors


class IngestionStage(object):
    def run(self):
        raise Exception("Contrived example")
        return 'Ingesting! Nom nom nom...'
```

Now if we run `python cli.py ingestion run` we will see the following error:

```s
$ python cli.py ingestion run

--------------------------------------------------------------------------------------------------------------
cli.py 67 <module>
fire.Fire(Pipeline)

core.py 141 Fire
component_trace = _Fire(component, args, parsed_flag_args, context, name)

core.py 466 _Fire
component, remaining_args = _CallAndUpdateTrace(

core.py 681 _CallAndUpdateTrace
component = fn(*varargs, **kwargs)

cli.py 9 run
raise Exception("Contrived example")

Exception:
Contrived example
```

A much nicer and easier-to-debug output error.

## Summary

Today's post demonstrated how to use the [`PrettyErrors`](https://github.com/onelivesleft/PrettyErrors) to make our error debugging experience much more pleasant.

## Resources and further reading

1. [The ABCs of Pipenv](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv)
2. [`PrettyErrors`](https://github.com/onelivesleft/PrettyErrors)
3. [Pipenv](https://pipenv.pypa.io/en/latest/)
4. [CLI Prompts In Python](https://blog.dennisokeeffe.com/blog/2021-07-29-cli-prompts-in-python)

_Photo credit: [`basilsamuellade`](https://unsplash.com/@basilsamuellade)_


_Originally posted on my [blog](https://blog.dennisokeeffe.com/blog/2021-07-30-pretty-error-output-in-python). To see new posts without delay, read the posts there and subscribe to my newsletter._