Python asynchronous/threads debugging in Visual Studio Code - python

I'm using Visual Studio Code for writing my python application. Inspecting variables and things like setting a watch in the left debugging pane works OK if I write a simple synchronous one-file program.
However, I have code with classes in multiple files, and use various callbacks from other modules. Some which start a new thread.
One of those modules is canopen
I can step thru my code, but when I enter the second line (below)
can0 = canopen.Network()
can0.connect(channel='can0', bustype='socketcan')
then the call stack changes from:
CALL STACK paused on breakpoint
main
<module>
to
CALL STACK paused on breakpoint
MainThread
Thread#15034.........
and simultaneously
the variables pane clears
and the watches in my watch window shows:
can0: not available
How can I (setup VS studio code with Python to) inspect/debug my python code with various threads and code in various files?
Regards,
Bas

New debugger is capable of debugging async applications.
Check out how to set it up
How to update ptvsd used by Visual Studio Code in debug mode
And don't forget to add "subProcess": true, in launch.json

Set the debug to works with Visual Code.
You can see a good article about how you can set the debugger here and here, see:
The setting "stopOnEntry":true will cause the debugger to break at the
first line of the python program being debugged. If this is not
desired, then change the value from true to false. The default value
of this setting is true.

Related

Visual Studio Code Debugger - ask for CLI arguments

I use debugger for Python in Visual Studio Code. It works great, but in many cases I want to debug scripts which use different CLI arguments. It's very cumbersome to create an entry in lauch.json for each CLI argument group I want to debug.
I'm looking for a way for VSC to ask for CLI arguments which will be passed to the script hen debugger starts. I would be even better is there would be some history of previous arguments.
PS. I know you can attach the debugger and run it via the command line, but I hope there is a clean solution :)
About your question:
I'm looking for a way for VSC to ask for CLI arguments which will be passed to the script hen debugger starts.
I'm sorry to tell you that the request can't be met. For now, there're two ways to debug showed in python-debug:
Add configurations in the launch.json, then press Run->Start Debugging, which will create a debug process automatically that provided by the python extension. During this debug process, all debug configurations depend on the content in launch.json:
Command line debugging. The debugger command line syntax you can refer to commandline-debug. Compared to changing configurations in launch.json every time, this is more convenient.
More information about debug in VS Code, please refer to Debugging

Visual Studio Code - python console

I'm using visual studio code with standard python extension, my issue is that when I run the code the python interpreter instantly closes right after and I only see the output which means that if I create some data structure I have to create it every single time. Is it possible to leave the console open after running the code and maybe running multiple files in the same python interpreter instance?
I used to use spyder which is entirely doing what you want (probably like PyCharm)...
Then I briefly tried VS Code, and it is quite easy to make it behave that way too.
First make sure you have an integrated terminal open (or do Ctrl+`, or View > Integrated Terminal), then in that terminal launch ipython.
Now, when you use the commands from Don Jayamanne's Python extension (Ctrl+Shift+P to open commands palette):
"Run Python File in terminal"
"Run select/line in Terminal"
It will run the line inside the ipython console directly. So running the entire file will call python module.py inside ipython, and thus fails.
So to make this work simply create settings to map which command is executed when "Run select/line in terminal":
Open Language specific settings (Shift+Ctrl+P, search "Configure Language specific Settings...")
Pick up Python
Now I would suggest to make change only in your workspace settings (top-right tab) to keep default behaviour in other cases
so add in WORKSPACE SETTINGS:
(keep in mind it is just a simple/stupid workaround)
{
"python.pythonPath": "run"
}
Now when runing whole file, it will use ipython function run within the ipython terminal that we launched, thus keeping all your workspace variables.
Also, if you run some line of code with "Run Select/Line in Terminal", the ipython session of that terminal keep all variables.
This allows live debugging, without actually going in the debug mode.
When you run a program, it runs until it ends. Then it closes. If you want it to stay live longer, you can make a program which does not stop until told so, e.g.
while True:
something = raw_input('Write something: ')
print('You wrote: %s' % something)
if something == 'bye':
print 'bye.'
break
This will run until user writes "bye".
I'm quite late to this conversation, but a workaround I use is to put a pass statement at the end of my file, then add a breakpoint to it. I then run it in the debugger and can access all of the variables etc.
This allows most of the functionality that I used to use in the PyCharm python terminal, such as exploring data structures, checking out methods, etc. Just remember that, if you want to make a multi-line statement (eg. for a loop), you need to use Shift-Enter to go to the next line otherwise it'll try to evaluate it immediately.

Where to Run Scripts at a PyCharm Debugger Breakpoint?

When running the PyCharm debugger and having stopped at a break point, is there a console to run and display some computation with the local variable exposed to me?
What I have in mind is the debugger setup in Matlab, where when stopped at a break point in a function, one can run scripts using the variables inside the function.
If I have understood well what do you mean, yes, PyCharm provides you a console where you can run local scripts or just see the variable's value at breakpoint time.
Start PyCharm debugger and at the bottom of your window you should see the main console.
You have to press a botton named "Show Python Prompt". Now, you should be able to write in console.
Look at the picture for more details.
You can use Evaluate Expression and Watches features to run code while debugging.

Debug C++ code running from python script in Eclipse

I have a C++ project and a python script that calls .exe file. I set breakpoints in one of cpp files, but they are not hit. I can't even go into this C++ project.
I found similar question here, but for Visual Studio:
Debug C++ code in visual studio from python code running in eclipse
Is there a similar way to do this trick in Eclipse?
Or is there some other approriate way to debug C++ code, which is called from another process? I tried to attach to the process, but the problem is that, after script makes call, child process finishes very fast and I can't catch its pid.
To debug the executable, use the DebugBreak function in place of a breakpoint in your program. So in your C++ code, call DebugBreak somewhere in your program, preferably near the beginning -- this will serve as the breakpoint.
What will happen is that when your C++ executable runs from Python, your C++ executable will "crash" (not really crash, but it will seem like it did). What really is happening is that the DebugBreak function was called.
After the Windows OS goes through its set of message boxes saying "your application has something wrong with it" (note that there really is nothing wrong with the program), you are then given the option to debug it. Choose the "debug" option, the debugger you want to use (which will be Visual Studio), and you should be able from that point on to single step through your C++ code.

Console Window Appears while debugging Python code using PTVS in Visual Studio

I have integrated PTVS into Visual studio so that i can have intellisense support and debugging capability.
I set Breakpoints at Function definitions, but when i debug the control goes directly out of function. And in some points the Console window pops up and it never iterates to the next line of code.
I liked PTVS but this thing has stuck me up.
In options->Python tools-> Interpreter options i have set it as Python 2.7
Can Anyone tell me whats wrong with the options and why that console screen is appearing.
Thanks in advance.
When you say you're setting breakpoint at function definitions do you mean on the line with the "def ..." or are you setting the breakpoint on the 1st statement of the function?
In Python functions are executable statements so if you're putting the breakpoint on the def line then you're going to hit the breakpoint when the function is being defined rather than being executed.
As far as the console window it will generally open unless you mark your app as a Windows application in project properties (this will launch pythonw.exe which doesn't include a console window).
If that doesn't help you might want to post the code you're having trouble with or a screenshot of the code with where the breakpoints are set.

Categories

Resources