11  Debugging with search engines

Many programmers use search engines to help them understand and resolve error messages. But there is some skill to crafting the right query, identifying relevant sources, and adapting them to your own code. Let’s start with an example error message.

Code
filename = "the-complete-works-of-shakespeare.txt"

line_counter = 0
with open(filename, encoding='UTF-8') as file:
    for line in file:
        line_counter += 1
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[6], line 4
      1 filename = "the-complete-works-of-shakespeare.txt"
      3 line_counter = 0
----> 4 with open(filename, encoding='UTF-8') as file:
      5     for line in file:
      6         line_counter += 1

File ~/Teaching/mads_onramp/workspace_files/resources/Course3/.venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py:324, in _modified_open(file, *args, **kwargs)
    317 if file in {0, 1, 2}:
    318     raise ValueError(
    319         f"IPython won't let you open fd={file} by default "
    320         "as it is likely to crash IPython. If you know what you are doing, "
    321         "you can use builtins' open."
    322     )
--> 324 return io_open(file, *args, **kwargs)

FileNotFoundError: [Errno 2] No such file or directory: 'the-complete-works-of-shakespeare.txt'

11.1 Strategy

11.1.1 Create a query.

How much of the error message and code should we use as our search query? Usually, a good place to start is with the last line of the traceback- which will contain important keywords like the error type (here, ‘FileNotFoundError’).

That’s the line:

FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] No such file or directory:  'the-complete-works-of-shakespeare.txt'

We might also consider dropping the-complete-works-of-shakespeare.txt, since that’s particular to our program and our data. It’s not likely to be a useful keyword to help us find other similar code problems that people have discussed online.

Let’s try Googling “FileNotFoundError: [Errno 2] No such file or directory:”.

11.1.2 Pick a starting place.

A great place to look is user forums like StackOverflow or Reddit, where communities of programmers can rate the quality of posts.

Let’s say we pick the first StackOverflow page. There are a few things I’m looking at here for signals:

  • When was the question asked?
  • Does the post look well-reviewed by the community?
  • Does the code example look relevant to me?
  • What’s the top-rated response?
  • What about the next few responses? Are there any interesting qualifications made there?
  • Do any of these explanations inspire a hypothesis about the proximal cause of the error?

11.1.3 Translate solutions to your code.

Because these types of discussions are usually around example code, you will have to translate any suggested code fixes to work with your specific code. That can mean translating the variable names offered in examples to your variable names.

11.1.4 Don’t just copy and paste- understand.

If your debugging strategy involves trying things until the error message goes away and then quickly moving on, you have no way of knowing if your fix is really satisfactory! Remember, the real goal is to better understand your program and the programming language.