How to debug Python running on a local lighttpd server using Python Tools for Visual Studio (PTVS)

StackOverflow https://stackoverflow.com/questions/10876048

  •  12-06-2021
  •  | 
  •  

Question

Visual Studio has great python support via the Python Tools for Visual Studio: http://pytools.codeplex.com/

Debugging python code in Visual Studio works as expected. I set up a lighttpd server on my local Windows machine, and I have python scripts running as cgi scripts. I would like to be able to debug these in Visual Studio (running on the same PC) when running from lighttpd.

I am able to debug the cgi scripts using: http://winpdb.org But I would prefer Visual Studio.

According to this question: How to debug a remote python application with (Python Tools for) Visual Studio?

It is implied there is almost a way, but I don't see a specific way to set a breakpoint and have it debugged in VS.

Was it helpful?

Solution

Figured it out. This does work, and works pretty well, once you get the steps down.

  • Add a delay to the python source file like this:

    time.sleep(10)
    

    This is necessary if you are not using FastCGI, as the python process is probably going to start and finish long before you can attach to it. I assume this would be different with FastCGI, but I haven't reached that stage yet.

  • Set a breakpoint in your python file that is running in your lighttpd (or apache?) server. I tested lighttpd. Note: If you don't add the source file to a project in VS, and thus don't set a breakpoint, you can still debug, but you'll have to force an error in the file. I added a line 'debugger' which worked.
  • Load the page in a browser and allow the delay to be hit
  • In VS: Debug -> Attach to process -> pythonw.exe (complete this before the delay elapses)

I added a short macro to the Visual Studio IDE that automates the procedure of attaching to pythonw.exe. I'll share that if anyone is interested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top