I want to debug with Python

It's nice that Python displays a stack trace when an error occurs, but I'm not sure how many minutes it's wrong.

So make a note of how to debug with a Python script.

The following site summarized it very easily, but since there was no detailed explanation about debugging terms, I will add a supplementary explanation. http://racchai.hatenablog.com/entry/2016/05/30/070000

Debugging in Python uses the pdb module.

Easy to use. Just add the line below just before the dubious place.

import pdb; pdb.set_trace()

If you execute the script in this state, the interactive debugger will start at that location. After that, just type the step command and execute it line by line. (This is called ** step-in **) Also, if you want to see the contents of a variable while debugging, use the p command. You can check the value stored in hoge by typing p hoge.

Command quick reference table

command Description Remarks
s(tep) Step in (execute line by line) If you enter a function during processing, it will stop line by line in the function as well.
n(ext) Step over (executed line by line) Execute one line at once including function call
r(eturn) Step out (execute in function units) Execute until the runtime function is returned
l(ist) View source before and after current line
a(rgs) Show the arguments of the current function
p Check the value of a variable p hogeUse like
c(ontinue) Run to next breakpoint

Reference site

http://docs.python.jp/2/library/pdb.html http://racchai.hatenablog.com/entry/2016/05/30/070000

Recommended Posts

I want to debug with Python
I want to analyze logs with Python
I want to play with aws with python
I want to use MATLAB feval with python
I want to make a game with Python
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to handle optimization with python and cplex
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I want to do ○○ with Pandas
I want to run a quantum computer with Python
I want to be able to analyze data with Python (Part 3)
I want to specify another version of Python with pyvenv
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
[Python] I want to use the -h option with argparse
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to use jar from python
I wanted to solve ABC160 with Python
I want to build a Python environment
I want to pip install with PythonAnywhere
I wanted to solve ABC172 with Python
I tried to debug.
I want to use a wildcard that I want to shell with Python remove
I want to monitor UNIQLO + J page updates [Scraping with python]
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to output the beginning of the next month with Python
I want to do a full text search with elasticsearch + python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to do Dunnett's test in Python
I want to analyze songs with Spotify API 2
I wanted to solve NOMURA Contest 2020 with Python
I want to create a window in Python
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to display multiple images with matplotlib.
I wanted to install Python 3.4.3 with Homebrew + pyenv
I want to be an OREMO with setParam!
I tried to get CloudWatch data with Python
I want to analyze songs with Spotify API 1
I want to merge nested dicts in Python
I don't want to use -inf with np.log
I want to use ip vrf with SONiC
I want to start over with Django's Migrate
I want to sell Mercari by scraping python
I want to make C ++ code from Python code!
I want to display the progress in Python!
I want to do it with Python lambda Django, but I will stop
I want to tweet on Twitter with Python, but I'm addicted to it
Environment maintenance made with Docker (I want to post-process GrADS in Python
I tried fp-growth with python
I tried scraping with Python
Connect to BigQuery with Python
I made blackjack with python!