Python Debugging in Vscode - make it similar to PyCharm? - python

I'm trying to learn Python and if you look 5-7 minutes into this tutorial he's able to - in a very illustrative way - debug (or "follow") his code. I'm unable to replicate this in a similar way in Vscode.
https://www.youtube.com/watch?v=akcEaEH91gI
Is it not able, or is it done in another way?
If you look at my screenshot. Nothing is added inside the script-console or the debugging console - as in PyCharm. Meanwhile, I get some data to the left but it's not as illustrative.
https://i.imgur.com/1J6N138.png
Not bashing Vscode, just trying to learn. :) Thanks

For the requirements you mentioned, please refer to the following:
About viewing variables.
In VSCode, we can view the variables in the code in the following two places:
1). "VARIABLES" on the left side of "Side Bar":
2). Right-click the mouse and click "Run Current File in Ineractive Window":
About "DEBUG CONSOLE".
When we use the setting "console":"internalConsole" in "launch.json", the execution result of the code will be displayed in "DEBUG CONSOLE", and VSCode will output in "integratedTerminal" by default.
In addition, since they are different software, we can't require them to be exactly the same in use. But if you encounter any usage and technical problems when using VSCode, please let us know.
More reference: The use of VSCode.

Related

Cannot get VS Code to pass arguments to Python from launch.json

I cannot figure out how to get VS Code for Mac to start debugging my Python script with the arguments I have defined in my launch.json file by using the debug button to the right I have circled here. The only way I can get my arguments to be recognized is if I go to the menu and select "Run|Start Debugging" or if press F5.
How do I tell VS Code to use my launch.json file when using that button???
If you look at the answer from Sourya Dey at the link below, this is what made me write this. There must be some simple setting or step missing to link that button to F5. It appears others are also confused about this.
Visual Studio Code: How debug Python script with arguments
For clarity, here is my launch.json file which works as expected with F5. It does not work with the button I have circled in red above
Update May 2022 from Microsoft tech support on github issues for vscode-python
You need to add this section to your launch.json file:
"purpose": ["debug-in-terminal"]
I just tested it on VS Code 1.67.1 and it works. The documentation appears to state that if you add this "flag" then F5 or Run > Debug won't use the launch.json settings but they do. I have asked them to update their docs to make this clearer as it is confusing.
============
I filed a bug with the Microsoft VS Code team regarding this issue and they sent me over to the Microsoft Python extension group. I then filed a bug there referencing the VC Code ticket and it was immediately assigned for "area-debugging triage". I presume this will be fixed in a later version (I am on 1.63.2)
First, it is not obvious where this button comes from. I have noted this issue as well with Microsoft so they can improve their documentation. When you install VS Code with no extensions, this button is not present. But as soon as you load your first Python file, Microsoft urges you to install the official "Microsoft Python Extension" and then the button shows up. In my mind, I assumed this was a native VS Code button that showed up when you had a language debugger installed. When I removed all extensions, except the Microsoft Python extension, I was convinced this was part of VS Code.
The answer above incorrectly states that this is the correct behaviour for the button. It is not. Microsoft has confirmed this and if you read issue #2338 ("Documentation for python.terminal.launchArgs is unclear") there is a response in there stating:
"Unfortunately that's just how vscode works. When debugging all settings are read from launch.json and non-debugging settings.json is used
This applies to all languages (extensions) in vscode."
So for now, there is nothing much to do other than wait for this to be fixed.
Lastly, for those who are wondering why I wanted to use that button in the first place. For the most part, I use keyboard mappings to start debugging when I am inside VS Code. But for this particular project, I was going to other external windows and when coming back to VS Code, since I was already using the mouse, I just wanted to click there out of convenience. I don't keep the debug tab open which also has a debug button that works as intended.

VScode run code selection

I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
Thanks.
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.
You can:
open a terminal at Terminal>New Terminal
Highlight the code you want to run
Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3 or python since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.

Debug window in jupyter?

I just started using jupyter for a python project. I constantly find myself adding an extra cell just to perform some basic try&error debugging. This way I omit the whole code of the cell is being executed but it still doesn't feel like the right way to do it.
Does Jupyter provide something like a static kernel terminal, for example always visible at the bottom of the screen, where I can simply paste code and execute runtime variables?
By the way: I did search but didn't find anything looking for static console, and terminal. Maybe I'm just looking in the wrong direction.
Thanks!
The jupyter console command will provide you with an interpreter environment that you can experiment with code running within the Jupyter environment outside of a notebook.
It's not exactly what you're looking for but may provide a better environment for testing and developing code that you can then paste into the appropriate notebook.

How to run code selection in Spyder without changing the active window to IPython console?

This is mostly a question regarding work-flow in Spyder, and not a pure programming question. If anyone thinks it has no place here, then please let me know and I'll remove it.
Here we go:
I'm running Spyder on a Windows 64-bit operating system. The interaction between the Spyder IDE and IPython console depends on how you choose to run your code, and I would like to specify how this interaction works.
Among several options to run parts of the code, the difference between the following two is what puzzles me:
Option 1 - Select parts of the code and hit F9
This runs the selected code. No surprise there.
Hit F9 again, and the same selection is executed once more.
Option 2 - Run the same selection using Cells (#%%) and Ctrl+Enter
The code is run just like before. But hit Ctrl-Enter again, and nothing happens. Probably because the IPython console now is the active window.
I would like to find some way of changing this so I can use Ctrl+Enter and run the cell contents multiple times without having to make the Spyder IDE the active window by manually clicking on it. I don't know if this is possible at all. Googling and messing around in the settings under Run / configure and Tools / Preferences has so far been useless.
Thank you for any suggestions!
P.S
In case you're wondering why this is such a big deal to me, I'm rendering multiple charts and exporting them to Power Point with minor changes each time the code is run. This way, new parts of the charts (lines, textboxes, etc) are revealed when I'm working my way through a finished Power Point presentation. And for my basic data science needs, this has turned out to be a pretty much optimal work-flow. Except, of course, that I have to manually select the Spyder IDE each time I run different selections of the code.
In the latest spyder version, the keyboard shortcut to run the highlighted lines of code is F9 but it doesn't seem to work.
Go change it from TOOLS>>PREFERENCES>>KEYBOARD SHORTCUTS then double click on any shortcut you want to change and change with your shortcut. But be very careful if you used the same shortcut that is used for any other work then for that it will remove.
I changed the shortcut for 'run selection' from F9 to ctrl + Enter
in Option 2, you should use Shift+Enter instead

PyDev interactive console integration with Variables view (Debug perspective)

My issue with the interactive console is twofold:
When I set a breakpoint in my python code, the execution pauses as expected at the breakpoint and displays all my variables in the "Variables" view. However, the interactive console is not very interactive anymore. I would like to be able to play around with the variables when execution is still paused at the breakpoint.
Ideally I would like to have this same behaviour if I'm not debugging but just working in the interactive console. Is there a way to couple the interactive console to the "Variables" view of the "Debug" perspective. When I open an interactive console now the variables view remains empty.
I am running a fresh install of Eclipse Juno (4.4.0) with PyDev (3.7.0).
I am using the latest pydev and I find the interactive console is still interactive :-) Note that no encouraging prompt is present at the console (e.g. no ">") but if you type one of the variables you see in the variables window you will get a value.. can manipulate etc.
My terminology might be a bit lose. If you mean by interactive console the full ">" console then it is tricky to get that to work during debugging. There is a pydev variable you can set to link it to the debug session but I find it a hassle still.. you have to explicitly switch to such a console.. given a command.. it then throws you back to the normal debug console (which is the one I was referencing as still sensitive to typing variable names etc).. Perhaps I am doing something wrong though for it to be so awkward. I posted on this a few weeks back but there was no reply. I too would like to do debugging in the full console with no hassle. In particular I would like to be able to use its command history to more efficiently manipulate things.
But regardless you can still debug and look at variables just not with the full feature console easily.
Also be aware there seems to be a bug lately (last few releases) where the variables view stays blank. I find that if I close it and reopen it then the variables appear.
Good luck

Categories