Journal Articles
Browse in : |
All
> Journals
> CVu
> 265
(10)
All > Topics > Programming (877) Any of these categories - All of these categories |
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.
Title: Debuggers Are Still For Wimps
Author: Martin Moene
Date: 04 November 2014 18:52:12 +00:00 or Tue, 04 November 2014 18:52:12 +00:00
Summary: Frances Buontempo shows how to remote debug python from Visual Studio.
Body:
Suppose you have a script you want to run on Linux and you only know how to drive the Visual Studio debugger. By installing an add-in for Visual Studio locally, installing the python tools for Visual Studio debugging on the remote machine, e.g. with pip install ptvsd==2.0.0pr1 and adding a (minimum of) a couple of lines to your script you can debug in Visual Studio even if the remote machine is running Linux. The additional lines are highlighted in the script in Listing 1.
#!/usr/bin/python """ You will need to insert both these in your script The remote box requires the ptvsd package (otherwise the import fails) """ import ptvsd ptvsd.enable_attach(secret = 'joshua') #use None instead of joshua but that is not secure #The secret can be any string - but this is not #properly secure def say_it(it):  """  This inserts a breakpoint  but you can add new breakpoints in Visual Studio  if required too/instead  """   ptvsd.break_into_debugger()  print(it) if __name__ == "__main__":  #pause this script til we attach to it   ptvsd.wait_for_attach()  say_it("Hello world") |
Listing 1 |
Be wary of line endings in VS, which may be inappropriate for Linux. More details are available online. [1] You’ll also need to install the ptvs from the relevant msi for your version of Visual Studio. Then start the script on the Linux box:
$python VSPyNoodle.py
It will hang, since it has a wait_for_attach call in main. ctrl-Z will stop it on the remote box if something goes wrong.
Select ‘Attach to process’ in the Debug menu on Visual Studio, and change the ‘Transport’ to ‘Python remote debugging (unsecured)’. Add the secret (joshua in this script) @ hostname to Qualifier, for example:
joshua@hostname
Hit ‘Refresh’. It should find the process running on the Linux box and add the port it uses to the Qualifier. Select your process in the list box and hit ‘Attach’ then debug as you are used to in VS.
If it complains about stack frames and not being able to see the code you may need to make a VS project from a local version of the code. having made sure it exactly matches the remote code.
Reference
[1] See https://pytools.codeplex.com/wikipage?title=Remote%20Debugging%20for%20Windows%2C%20Linux%20and%20OS%20X
Notes:
More fields may be available via dynamicdata ..