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 following script:
#!/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")
See https://pytools.codeplex.com/wikipage?title=Remote%20Debugging%20for%20Windows%2C%20Linux%20and%20OS%20X for more details and be wary of line ending in VS which may be inappropriate for linux.
Install the ptvs from the relevant msi for your version of Visual Studio.
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
Change the "Transport" to "Python remote debugging (unsecured)"
Add the secret (joshua in this script) @ hostname to Qualifier
e.g. 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.
Thank you for sharing this! What you have shared is very helpful and informative. Would love to see more updates from you.
ReplyDeleteWebsite Development