Back

Debugging Python In Visual Studio

Hi there,

I have done all what is necessary to be able to debug attach using VS 2019 latest version but am unable to do so. I think the problem is with the VS debug adapter. I am unable to attach although I see Lean waiting for me to do so. Consequently I am unable to develop Lean in python!

Is there a way to launch the python -lean runtime without using the .net Launcher??

Thank you for any thoughts

Fig

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hey Fig,

If you are using the ptvsd debugger method, can you provide specs about the Python environment and DebugPy version you are using? This can help us determine the problem.

If not, can you describe in more detail the steps you are taking to debug an algorithm? The Launcher is required to run Lean, but I'm sure we can get you started with Lean once we figure this out!

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Jack,

How kind of you. Yes, I am really struggling with this. It used to work too.

I am using the latest VS 2019 and the basic template to keep things simple..

import ptvsd
#ptvsd.enable_attach(secret = 'dev', address = ('localhost', 2019))
#tcp://localhost:5678 
ptvsd.enable_attach()
print(f'Python Tool for Visual Studio Debugger {ptvsd.__version__}')
ptvsd.wait_for_attach()

Trace:: Python for .NET Assembly: Python.Runtime, Version=1.0.5.29, 

I have also tried ptvsd.break_into_debugger()

But again, no joy.

I appreciciate any thoughts or suggestions.

Fig

 

0

Hey Fig,

So I'll walk you through the steps that work for me, and if it doesn't work for you or these are the steps you are already following, then we can continue debugging.

First step is to make sure your config file is setup correctly. In this, I'm debugging the Python BasicTemplateAlgorithm.
68066_1585237467.jpg

The next step is to do the ptvsd import and setup. The ptvsd.break_into_debugger() shouldn't be needed, although you can use it to break it. I just set a normal breakpoint where I want it to step into the debugger (line 41).

68066_1585237506.jpg

Next, you want to debug it without attaching it to a process yet. You can use Ctrl-F5, or go to Debug > Start Without Debugging. This will bring up this window.
 

68066_1585237659.jpg

Once you see the line underlined in red, you want to hit Ctrl-Alt-P. This will bring up a window where you will attach to a process. Set the process to the following, hit enter, and then click Attach.

68066_1585237698.jpg

Once you hit Attach, this window will close and bring back up the console window. Lean will then continue to run until it hits the breakpoint or ptvsd.break_into_debugger() you set. After this, you can treat it like the normal VS debugger, although you won't be able to step into any code other than the file you're running, whereas using C# allows you to step into code in other files.

68066_1585237780.jpg
 

Hopefully, this helps. If it doesn't work or this is what you were already doing without it working, let me know and we'll find another way.

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Jack,

Thank you for your kind help. 

I followed your instructions and they were very clear (thanks) but I get the same strange error when clicking on the debug target from the VS attach to process dialog:

"Path to debug adapter executable not specified.

The program '[54321] Python @ tcp://localhost:5678/' has exited with code -1 (0xffffffff)."

I don't have any other information to go on. But I see Lean waiting for the attachment. How stange.

Thanks again though!

Fig

0

Lee -- mind 1) making sure your firewall allows access to that port, and then 2) testing this? 

RUN pip install "ptvsd==3.2.1"https://github.com/Microsoft/PTVS/issues/4270


 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Jared,

Much obliged for your help with this. I checked the firewall and upgraded to the latest version of ptvsd.

I tested the debug attachement on a test python project and worked as expected. But I am still unable to debug ointo Lean. How baffling!

It can't be a timing issue but must have something to do with configuration. 

Could the experimental debugger be at fault perhaps?

Thanks

Fig  

0

By this "But I am still unable to debug into Lean." - do you mean to step into both python and LEAN C# classes? Python debugging only works in the python file. If you'd like to debug on the C# side will need to debug using the normal C# process (build in debug mode, set a breakpoint in C#).

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Jared, apologies for being rather unclear on this. I meant debug attachment to the C# based Lean Launcher running a python algorithm. I used to be able to do this but am not able to do so at present. Hence I am unable to develop python based strategies. But I am able to debug attach into other python projects using the VS ptvsd plugin. I would struggle to convert code to C# from python otherwise. 

0

We have a fix now I think...

I was able to attach process once the debug flag in the configuration file was set to false. Odd I know. 

Thank you both once again for all your help.

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed