Back to home

PrerequisitesGetting startedDemonstrating the errorPrettifying the errorSummaryResources and further reading
Pretty Error Output In Python main image

Pretty Error Output In Python

This post will use the PrettyErrors library to output more readable errors in Python.

It will build off the work done on learning Python Fire and PyInquirer basics in a previous blog post.

Prerequisites

  1. Familiarity with Pipenv. See here for my post on Pipenv.
  2. Familiarity with Python Fire
  3. (optional) Familiarity with PyInquirer

Getting started

While I will be using my hello-python-fire repo, the follow will work in any repo initialized with pipenv.

1 2 # 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:

1 2 3 4 5 6 7 8 9 #!/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:

1 2 3 4 5 6 7 8 9 10 11 12 13 $ 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:

1 2 3 4 5 6 7 8 9 10 #!/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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $ 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 to make our error debugging experience much more pleasant.

Resources and further reading

Photo credit: basilsamuellade

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.