{"id":3671,"date":"2018-02-12T16:22:15","date_gmt":"2018-02-13T00:22:15","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=3671"},"modified":"2018-07-06T12:26:29","modified_gmt":"2018-07-06T19:26:29","slug":"debugging-python-code-on-raspberry-pi-with-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/raspberry\/python3\/","title":{"rendered":"Debugging Python 3 code on Raspberry Pi with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to use Visual Studio with VisualGDB to debug the Python code running on Raspberry Pi remotely.<\/p>\n<p>As of February 2018,\u00a0the\u00a0pre-built Python package provided with the Raspbian distro contains optimizations\u00a0preventing the debugger from interpreting the Python frames correctly.\u00a0In this tutorial we will show how to build a\u00a0debuggable version of Python on your Raspberry Pi and use Visual Studio to debug scripts running under it.<\/p>\n<ol>\n<li>Locate the source package for the latest Python version on the <a href=\"https:\/\/www.python.org\/downloads\/source\/\">downloads page<\/a>. In this tutorial we will use Python 3.6.4 (requires VisualGDB 5.4) as\u00a0 version 3.7.0 doesn&#8217;t\u00a0build correctly on Raspberry Pi. If you are using VisualGDB 5.3 or earlier, please use Python 3.5 instead.<\/li>\n<li>Start your Raspberry Pi and connect to it using <a href=\"http:\/\/smartty.sysprogs.com\/\">SmarTTY<\/a> or any other SSH client.<\/li>\n<li>If your Python code depends on SSL support (e.g. required to use pip),\u00a0install the libssl-dev package before you begin building the new Python environment:\n<pre class=\"\">sudo apt-get install libssl-dev<\/pre>\n<\/li>\n<li>Run the following commands to download, unpack and build the Python interpreter:\n<pre class=\"\">wget https:\/\/www.python.org\/ftp\/python\/3.6.4\/Python-3.6.4.tgz\r\ntar xf Python-3.6.4.tgz\r\ncd Python-3.6.4\r\n.\/configure &amp;&amp; make -j4<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-smart.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3672\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-smart.png\" alt=\"01-smart\" width=\"817\" height=\"456\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-smart.png 817w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-smart-300x167.png 300w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/a><\/li>\n<li>Ensure that the initial build succeeds.\u00a0Now we will patch the\u00a0<strong>ceval.c<\/strong> file to allow the\u00a0debugger to interpret the Python frames. Open it in a\u00a0text editor and replace the function body with the following:\n<pre class=\"\">PyObject *\r\nPyEval_EvalFrameEx(PyFrameObject *f0, int throwflag)\r\n{\r\n PyFrameObject * volatile f = f0;\r\n PyThreadState *tstate = PyThreadState_GET();\r\n return tstate-&gt;interp-&gt;eval_frame(f, throwflag);\r\n}<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/02-patch.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3673\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/02-patch.png\" alt=\"02-patch\" width=\"817\" height=\"456\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/02-patch.png 817w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/02-patch-300x167.png 300w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/a><\/li>\n<li>Build the Python interpreter again (it will only rebuild 1 file) and install it:\n<pre class=\"\">make -j4\r\nsudo make install -j4<\/pre>\n<\/li>\n<li>The Python executable will normally be installed under \/usr\/local\/bin. You can double-check this by running &#8220;which python3&#8221; followed by &#8220;python3 &#8211;version&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/03-ver.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3677\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/03-ver.png\" alt=\"03-ver\" width=\"817\" height=\"456\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/03-ver.png 817w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/03-ver-300x167.png 300w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/a><\/li>\n<li>Start Visual Studio and open VisualGDB Linux\u00a0Project Wizard:<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3674\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-prj.png\" alt=\"01-prj\" width=\"840\" height=\"586\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-prj.png 840w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-prj-300x209.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-prj-392x272.png 392w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/01-prj-130x90.png 130w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><\/li>\n<li>On the first page of the wizard select the Python-based project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/02-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3675\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/02-py.png\" alt=\"02-py\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/02-py.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/02-py-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>On the next page click\u00a0&#8220;Build the project under Linux&#8221; and\u00a0select your Raspberry Pi in the &#8220;Remote computer&#8221; field:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/03-raspi.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3676\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/03-raspi.png\" alt=\"03-raspi\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/03-raspi.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/03-raspi-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>On the next page enter &#8220;python3&#8221; or the full path to the installed Python executable in the &#8220;Python executable&#8221; field. If you have the pre-packaged Python installed as well, we recommend entering the full path to avoid confusion:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/04-exe.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3678\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/04-exe.png\" alt=\"04-exe\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/04-exe.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/04-exe-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a>Press &#8220;Next&#8221;. If you are using VisualGDB 5.4 or later, it will automatically detect\u00a0Python-related settings and check that all the\u00a0necessary components are present. If you are using\u00a0version 5.3 or earlier, you may need to specify the Python header directory manually.<\/li>\n<li>Proceed with the default file synchronization settings. In this mode VisualGDB will\u00a0keep a local copy of all source files in the project directory and will upload the modified\u00a0ones before you begin debugging:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/05-access.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3679\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/05-access.png\" alt=\"05-access\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/05-access.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/05-access-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create the project. VisualGDB will generate a basic Python scripts\u00a0demonstrating the use of classes and variables:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/06-greet.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3680\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/06-greet.png\" alt=\"06-greet\" width=\"1090\" height=\"652\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/06-greet.png 1090w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/06-greet-300x179.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/06-greet-1024x613.png 1024w\" sizes=\"(max-width: 1090px) 100vw, 1090px\" \/><\/a><\/li>\n<li>Set a breakpoint in the Greet() function and press F5 to\u00a0begin debugging. VisualGDB will automatically upload your sources and launch Python remotely. Once\u00a0the breakpoint is hit, you will be able to debug your program as\u00a0if it was a regular C\/C++ program:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/07-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3681\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/07-bkpt.png\" alt=\"07-bkpt\" width=\"1090\" height=\"704\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/07-bkpt.png 1090w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/07-bkpt-300x194.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/02\/07-bkpt-1024x661.png 1024w\" sizes=\"(max-width: 1090px) 100vw, 1090px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use Visual Studio with VisualGDB to debug the Python code running on Raspberry Pi remotely.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[122,43],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3671"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=3671"}],"version-history":[{"count":5,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3671\/revisions"}],"predecessor-version":[{"id":4055,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3671\/revisions\/4055"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=3671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=3671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=3671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}