

{"id":144,"date":"2022-01-19T14:06:01","date_gmt":"2022-01-19T19:06:01","guid":{"rendered":"https:\/\/sites.temple.edu\/vahid\/?p=144"},"modified":"2022-01-19T14:58:16","modified_gmt":"2022-01-19T19:58:16","slug":"debugging-in-python","status":"publish","type":"post","link":"https:\/\/sites.temple.edu\/vahid\/2022\/01\/19\/debugging-in-python\/","title":{"rendered":"Debugging in Python"},"content":{"rendered":"\n<p>There are three main tools for debugging in Python.<\/p>\n\n\n\n<p>In Python version less than 3, it was common to use the set_trace() command for making a starting debugger point, but in newer versions, it is possible to define the <strong>PYTHONBREAKPOINT<\/strong> environment variable before running the Python program and the debugger can be started by the breakpoint() command.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>pdb<\/strong><\/li><\/ul>\n\n\n\n<p>This is the default debugger in Python. It is similar to gdb in Gnu Linux with very limited capabilities, but enough to debug programs in Python.<\/p>\n\n\n\n<p>It works in the terminal and by the &#8220;h&#8221; command, the list of available commands with brief descriptions appears.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>ipdb<\/strong><\/li><\/ul>\n\n\n\n<p>Colorful debugging with running the following command:<\/p>\n\n\n\n<p><code>python -m ipdb test.py<\/code><\/p>\n\n\n\n<p>Or calling from inside the Python code by:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import ipdb\nbreakpoint()<\/code><\/pre>\n\n\n\n<p>The commands are similar to pdb.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>PuDB<\/strong><\/li><\/ul>\n\n\n\n<p>This is a graphical debugger that runs in the terminal. The graphical feature makes it attractive and the usage is very similar to ipdb.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"805\" height=\"449\" src=\"https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/pudb.jpg\" alt=\"\" class=\"wp-image-148\" srcset=\"https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/pudb.jpg 805w, https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/pudb-300x167.jpg 300w, https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/pudb-768x428.jpg 768w\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>web-pdb<\/strong><\/li><\/ul>\n\n\n\n<p>As the name shows, it works in the browser. The URL of localhost:5555 is used which can be redirected via ssh with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -X username@server -L 5555:localhost:5555<\/code><\/pre>\n\n\n\n<p>At the top of the program, the following commands should be inserted:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><code>import web_pdb\nweb_pdb.set_trace()<\/code><\/code><\/pre>\n\n\n\n<p>Or changing the environment variable as PYTHONBREAKPOINT=&#8221;web_pdb.set_trace&#8221; and then calling the breakpoint() command.<\/p>\n\n\n\n<p>Then the debugger can be opened in the web browser.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"726\" src=\"https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/Screenshot_2022-01-19_13-47-23-1024x726.jpg\" alt=\"\" class=\"wp-image-145\" srcset=\"https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/Screenshot_2022-01-19_13-47-23-1024x726.jpg 1024w, https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/Screenshot_2022-01-19_13-47-23-300x213.jpg 300w, https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/Screenshot_2022-01-19_13-47-23-768x545.jpg 768w, https:\/\/sites.temple.edu\/vahid\/files\/2022\/01\/Screenshot_2022-01-19_13-47-23.jpg 1393w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are three main tools for debugging in Python. In Python version less than 3, it was common to use the set_trace() command for making&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/sites.temple.edu\/vahid\/2022\/01\/19\/debugging-in-python\/\">Continue reading<span class=\"screen-reader-text\">Debugging in Python<\/span><\/a><\/div>\n","protected":false},"author":18759,"featured_media":145,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[94,96,95,98,97,99],"class_list":["post-144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tools","tag-debug","tag-debugger","tag-debugging","tag-ipdb","tag-pdb","tag-web-pdb","entry"],"_links":{"self":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/users\/18759"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":0,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/media\/145"}],"wp:attachment":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}