Debugging Your Python Code: For Dummies

Similar documents
Introduction to Programming

Intro. Scheme Basics. scm> 5 5. scm>

The compiler is spewing error messages.

Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

5 REASONS YOUR BUSINESS NEEDS NETWORK MONITORING

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

This book is about using Visual Basic for Applications (VBA), which is a

Implementing an Algorithm for Boomerang Fraction Sequences in Python

Divisibility Rules and Their Explanations

4 KEY FACTORS FOR DATA QUALITY ON A DATA LAKE (OR: HOW TO AVOID THE DATA SWAMP) JOSH HERRITZ MIOSOFT CORPORATION MIOsoft Corporation.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

An Introduction to Python

CS354 gdb Tutorial Written by Chris Feilbach

T H E I N T E R A C T I V E S H E L L

Part II Composition of Functions

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

Project Collaboration

Module 1: Introduction RStudio

These are notes for the third lecture; if statements and loops.

COPYRIGHTED MATERIAL. Dipping Your Toe into Python. Part I. Chapter 1: Programming Basics and Strings. Chapter 2: Numbers and Operators

Testing and Debugging

Upgrading Your Geant4 Release

Clean & Speed Up Windows with AWO

XP: Backup Your Important Files for Safety

the NXT-G programming environment

Project and Production Management Prof. Arun Kanda Department of Mechanical Engineering Indian Institute of Technology, Delhi

Crash Course in Modernization. A whitepaper from mrc

Project 1 Balanced binary

If Statements, For Loops, Functions

Here we will look at some methods for checking data simply using JOSM. Some of the questions we are asking about our data are:

Animations involving numbers

Python RPM Porting Guide Release 0.1

Lab 7 Unit testing and debugging

Excel programmers develop two basic types of spreadsheets: spreadsheets

Lecture 3: Linear Classification

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

Installing Ubuntu Server

Civil Engineering Computation

SIS offline. Getting Started

Taskbar: Working with Several Windows at Once

This is a book about using Visual Basic for Applications (VBA), which is a

15 Minute Traffic Formula. Contents HOW TO GET MORE TRAFFIC IN 15 MINUTES WITH SEO... 3

LOOPS. Repetition using the while statement

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

1

Data Feeds Traffic Setup Instructions

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Rescuing Lost Files from CDs and DVDs

Close Your File Template

Text Input and Conditionals

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

Table of Laplace Transforms

Dynamism and Detection

Chapter 2.6: Testing and running a solution

Fourier Transforms and Signal Analysis

How To Test Your Code A CS 1371 Homework Guide

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

CS1 Recitation. Week 1

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

Lab #0 Getting Started Due In Your Lab, August 25, 2004

Lecture 9: July 14, How to Think About Debugging

Usability Test Report: Requesting Library Material 1

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Money Management Account

Heuristic Evaluation of Team Betamax

KNOWLEDGE FORUM 4 MACINTOSH SERVER ADMINISTRATOR S GUIDE

A Guide to Finding the Best WordPress Backup Plugin: 10 Must-Have Features

Worksheet Answer Key: Scanning and Mapping Projects > Mine Mapping > Investigation 2

Xchange for Samsung MAC User Guide. Version 2.4

4Sight for Mac User Guide. Version 2.4

Starting to Program in C++ (Basics & I/O)

User Defined Functions

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Variables and. What Really Happens When You Run hello_world.py

Burning CDs in Windows XP

Programming in the Real World. Dr. Baldassano Yu s Elite Education

Building a Django Twilio Programmable Chat Application

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

1 Lecture 5: Advanced Data Structures

Learn Python In One Day And Learn It Well: Python For Beginners With Hands-on Project. The Only Book You Need To Start Coding In Python Immediately

Chapter 5 Errors. Bjarne Stroustrup

It d really be nice if all the scripts you write work flawlessly each and

Variables, expressions and statements

How to approach a computational problem

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Creating a new form with check boxes, drop-down list boxes, and text box fill-ins. Customizing each of the three form fields.

Joopal and Drumla. Sam Moffatt, Joomla! September 13, 2009

6.001 Notes: Section 8.1

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Section 0.3 The Order of Operations

(Python) Chapter 3: Repetition

CSCI 1100L: Topics in Computing Lab Lab 1: Introduction to the Lab! Part I

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

A Guide to Condor. Joe Antognini. October 25, Condor is on Our Network What is an Our Network?

Get Your Browser into Use Quickly!

Input, output, and sequence

Sitecore Projects with GatherContent

Transcription:

Debugging Your Python Code: For Dummies Tyler J. Metivier University of Connecticut Dept. of Physics May 4, 2018 1 What s the problem? It doesn t matter if you ve written 1 script or programmed a space shuttle launch- everyone s code breaks at some point. What s more important is that you know how to interpret the error you ve encountered and recover. Tears won t suddenly make your Numpy array callable. Generally, there are three different types of errors in Python: 1. Syntax errors 2. Logic errors 3. Exceptions From personal experience, I can say that there are myriad ways to end up with one of the errors listed above- but there are more fundamental issues that could plague your code. Despite efforts to standardize the newest release of Python (3), many programmers and businesses have stuck with Python 2. The end-of-life date (the predetermined date in which the product becomes obsolete) for Python 2 has even been extended 5 years until 2020 because of how many people currently rely on it s support. So why don t people just update their code? 1

Python 2 and Python 3 are incredibly similar, but there are some large differences in how they accomplish tasks. Python 2 contains many built-in functions and dependencies that simply don t exist in Python 3. This can be a major problem if you want to work in Python 3 but your code is based on a function that doesn t exist anymore. So what if you run into this issue? Perhaps you were browsing Github and downloaded some interesting-looking code to you. You try to run it and... The terminal returns to us: the file name, the line that triggered the error, and the type of error, respectively. So we see that our file contains a syntax error in line 4 (more on syntax errors in section 2). By inspection, we can see that the line, print test, does NOT have parentheses around the argument: print( test ). We received this error because we tried running a code written in Python 2 on our system running Python 3 1. The line above is completely valid in earlier releases of Python, but now it isn t. This is one example of how Python 3 requires more order than its predecessors. Indentation and syntax matter in the long run when codes become complicated. Luckily, Python 3 comes with a very helpful script that you can call in any directory: 2to3. Now I could open my code and go through adding () after every print I see, but what if there are thousands of lines or more errors hiding? Accomplishing tasks systematically is generally much better practice than doing it manually. Looking at our original problem: We see the missing parentheses on line 4, just like we saw before. Now we can enter the following into the terminal (the file here is called test.py): 1 To find out which version of Python you have, type: python -v into the terminal and it will return the installation directory and version. 2

Python s 2to3 script uses something called a fixer to interpret code, identify what s outdated, and change it into working code. Fixers work by reading through code and identifying Python 2-specific blocks of text. When a piece of outdated code is recognized, 2to3 will scan through its database to identify what the Python 3- equivalent is. Simply running this program (as shown above) will rewrite your original file with the implemented fixes. However, 2to3 always saves a backup 2. If we run the same line again but with a -w after 2to3, this will restore the source file to its original form. As seen to the right, 2to3 subtracted the line that was causing trouble and then put it back with parentheses. This can be a big time-saver depending on what you re working with. Now if we reopen the file, we see that parentheses have indeed been added. But of course, most errors aren t because of conversion issues. The generalized type of error we saw here was a syntax error. Syntax errors are fatal - meaning that when one is encountered, the code will fail to execute and the script will stop being read at that line. Above, we were kicked out of the code at line 4 when Python didn t understand the print argument. When Python encounters a line of code it can t read, it is a syntax error. Above is an example of an incorrect argument. Syntax errors are also commonly due to typos or incorrect indentation (mixed use of spaces and tabs). 2 Adding a -n to your original call will generate no backup file- this is NOT recommended. 3

A more complicated situation is the logic error. Logic errors could crash your code or seemingly do nothing at all. They often go completely undiscovered because your code may appear to run perfectly fine. However, that doesn t mean that it s running correctly. Even though your code may appear to be fine, you may have used the wrong variable name in a function and your mathematical result is 5 orders of magnitude different than expected. Lastly, we have the exceptions. This occurs when Python correctly interprets the code you wrote, attempts to execute it, and for whatever reason cannot. For example, if my code relied on a web-based repository and I attempt to run it while on vacation at the Hubble Space Telescope- it may throw an exception due to me not having the HST wifi-password (because I m in space... get it?). Python knows what I want to do, it just can t do it because there s no internet connection. 2 How do I fix it? After identifying the type of error, we can begin to try and figure out how to solve the problem. Dealing with syntax errors is generally not a very difficult task. Because they are mostly comprised of typos, inconsistent indentation, and incorrect usage of arguments- we can start fixing the problem when we know where it is. Just like when we found the syntax error before, the terminal tells us the file that caused it and the exact place where the code breaks down. If the problem seems clear to you (made a typo, forgot parentheses, etc...)- simply attempt to implement the fix and rerun your code. If you can t identify the issue, try browsing a website like StackExchange to see if other people have encountered the same problem 3. Learning Python means learning how other people code and deal with their problems, as people often find better and easier ways to accomplish such things. The most frustrating part about logical errors is that they are occasionally invisible. If you ve encountered a logic error, the first thing you can do is look over your code entirely. The issue may be easy to identify- you may 3 Link: https://stackoverflow.com/questions/tagged/python 4

have meant x1 and accidentally put x!. Or your code may be comprised of many convoluted mathematical functions that would take an incredible amount of time to pour through. If you really want to rid your work of logic errors- the recommended course of action is to install a debugging software. Python comes with a debugging module pdb installed, so this would be the best one to try initially. We can debug a code of our choice with the terminal entry above. (Hopefully) the debugger will identify issues in your code and implement fixes. If all else fails, you can go line by line and try to make sense of what could be going wrong. Python will do exactly what you tell it to do- as long as you know what you re actually telling it to do. This is why debuggers can be helpful when the source of the error is unknown. In a similar fashion to 2to3, debugging software looks for identifiable pieces of code and attempts to implement corrections. Naturally, this method is not always perfect. In science, being mindful of your expected results is vital. If you run your program and receive a value, take time to consider if it s reasonable or makes sense given the situation. If you are expecting to receive a histogram with an exponential trend but instead receive: -you may initially want to run your fancy debugging software to fix the issue. But that won t help you whatsoever in this case. Nothing is actually wrong with your code. Instead, if we look at the generated plot and notice 5

that something s funky about the scale on the vertical axis- we are halfway through solving our problem. We may discover that a lonesome log=true was the culprit all along. Removing that argument then generates: We now have our expected exponential trend. But still there was nothing ever wrong with the code- we were just accidentally telling it to do something and it listened. Exceptions are the least frightening to deal with, as this means that your code is (probably) not broken. You are requesting something from Python and it understands your request- it just can t help you. That means it s all on you here. Once again, we must look and see where the terminal is telling us the error is. Once we ve found the location, we can implement fixes. If we find the exception is thrown because there isn t internet and we re asking Python to go to a website- we either need to get a connection or work around the issue. Creating an exception handling block can allow us to bypass exceptionrelated errors. This is most commonly done with a try-except block. An example of a try-except block is: 6

Under both try and except, we see we are ultimate attempting to call a text file and call it catalog. We first try to simply call the file from our system. If the file exists within your working directory, the catalog should be read with no problem. If this file doesn t exist, we attempt to call the file in an alternative method with except. As shown above, we first attempt to download the text file with os.system(...). Here, we are telling our computer to wget 4 (install from the internet) our file from a web page, dl base. In this specific situation, we can define dl base as some url to the website we want to get our file from. There is also /files/directory catalog 135.txt appended at the end of the line. This just specifies the exact file that we want to download from this website. If your computer doesn t have the file AND doesn t have internet... it may be time to call a local internet service provider. Despite the fact that Python errors come in many different forms, it s good to have a methodology when working through them. 1. Read your code thoroughly before attempting to run it. 2. Identify the type of error you may be receiving. 3. Based on the type of error, use the provided suggestions or research how to bypass your issues. 4. Never be afraid to use the internet as a guide- chances are that someone in the world has already encountered the issue you re facing. 4 Wget is a very common program that allows you to download content from the internet through the terminal/command line. (https://www.gnu.org/software/wget/) 7