I can run python script well on visual studio:
But when I set a “break point”, it never stop on this point and an error occurs:
I can set a “bread point” and make it work well in c# on visual studio, so how shall I do the same in python(In addition, I have installed “debugpy” and “ptvsd”)?
Fred Painchaud
Hi Frank,
When you run LEAN with a Python script in VS, you are running two executables in one: the .NET CLR and Python. Python cannot magically run alongside .NET/C# code. A somewhat invisible bridge between the two is included in LEAN and it is Python.NET. All C# code (LEAN) runs in the CLR and all Python code runs in the Python runtime, started by Python.NET.
Now, if you run LEAN in debug mode while including a Python script in it, and you breakpoint LEAN, the CLR stops - and so does your Python script as it is driven by LEAN. Since you usually want to debug your Python script, running LEAN in debug mode does not make much sense in that case. So, you may know all this but anyway, I go over it. Now, if you want to run Python and debug it, you need a way to tell VS that you want it to control the Python runtime and break when you placed a breakpoint. That way is through yet another bridge which is debugpy and ptvsd. ptvsd is old but last time I checked (a while ago), it was the only one supported. Maybe debugpy is now supported. Anyway, when you want to use them, you first want to configure the project so it uses debugpy/ptvsd as the debugger. Then you want to run LEAN not in debug mode. That starts the CLR. Since it is configured for a non-C# debugger, it will wait for it. You can read that in your log “debugpy waiting for attach at port 5678”. Then you go back to VS and in the run menu, you'll have attach. Once you click on it, the Python runtime will start and your breakpoints will be considered.
Fred
Frank cao
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!