Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Debugging a Click application using pdb#
This tip is for when you are working on a Python command-line application that runs using that program’s name, as opposed to typing python my_script.py. I usually need this when I’m working on applications built using Click, e.g. projects I start using my click-app cookiecutter template.
So you’re working on one of these apps and it throws an exception:
1% dogsheep-beta index beta.db dogsheep-beta.yml2Traceback (most recent call last):3...4 File "/Users/simon/Dropbox/Development/dogsheep-beta/dogsheep_beta/utils.py", line 46, in run_indexer5 columns = derive_columns(other_db, sql)6 File "/Users/simon/Dropbox/Development/dogsheep-beta/dogsheep_beta/utils.py", line 63, in derive_columns7 cursor = db.conn.execute(sql + " limit 0")8sqlite3.OperationalError: near "from": syntax errorYou can run the Python debugger against it using python -i $(which dogsheep-beta) to get an interactive prompt, then import pdb; pdb.pm() to open the debugger at the last exception:
1% python -i $(which dogsheep-beta) index beta.db dogsheep-beta.yml2Traceback (most recent call last):3 File "/Users/simon/.local/share/virtualenvs/dogsheep-beta-u_po4Rpj/bin/dogsheep-beta", line 11, in <module>4 load_entry_point('dogsheep-beta', 'console_scripts', 'dogsheep-beta')()5 File "/Users/simon/.local/share/virtualenvs/dogsheep-beta-u_po4Rpj/lib/python3.8/site-packages/click/core.py", line 829, in __call__6 return self.main(*args, **kwargs)7 File "/Users/simon/.local/share/virtualenvs/dogsheep-beta-u_po4Rpj/lib/python3.8/site-packages/click/core.py", line 782, in main8 rv = self.invoke(ctx)9 File "/Users/simon/.local/share/virtualenvs/dogsheep-beta-u_po4Rpj/lib/python3.8/site-packages/click/core.py", line 1259, in invoke10 return _process_result(sub_ctx.command.invoke(sub_ctx))11 File "/Users/simon/.local/share/virtualenvs/dogsheep-beta-u_po4Rpj/lib/python3.8/site-packages/click/core.py", line 1066, in invoke12 return ctx.invoke(self.callback, **ctx.params)13 File "/Users/simon/.local/share/virtualenvs/dogsheep-beta-u_po4Rpj/lib/python3.8/site-packages/click/core.py", line 610, in invoke14 return callback(*args, **kwargs)15 File "/Users/simon/Dropbox/Development/dogsheep-beta/dogsheep_beta/cli.py", line 30, in index16 run_indexer(db_path, rules, tokenize=None if tokenize == "none" else tokenize)17 File "/Users/simon/Dropbox/Development/dogsheep-beta/dogsheep_beta/utils.py", line 46, in run_indexer18 columns = derive_columns(other_db, sql)19 File "/Users/simon/Dropbox/Development/dogsheep-beta/dogsheep_beta/utils.py", line 63, in derive_columns20 cursor = db.conn.execute(sql + " limit 0")21sqlite3.OperationalError: near "from": syntax error22>>> import pdb; pdb.pm()23> /Users/simon/Dropbox/Development/dogsheep-beta/dogsheep_beta/utils.py(63)derive_columns()24-> cursor = db.conn.execute(sql + " limit 0")25(Pdb) print(sql)26...Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.